0

data

https://github.com/mwaskom/seaborn-data/blob/master/titanic.csv

Goal

  • round data by my defined function like below.

Problem

  • When df.age.map(float_round), it returns ValueError: cannot convert float NaN to integer.

I don't want to split data into null data and changed the not null data and then merge it. But using one function to achieve it.

from math import ceil, floor


def float_round(num, places=2, direction = ceil):
    return direction(num * (10**places)) / float(10**places)

Note: the round function could not achieve what I want. See this post .

Jack
  • 1,724
  • 4
  • 18
  • 33

1 Answers1

1

Easiest fix would be to use np.ceil/np.floor which are both NaN safe:

import pandas as pd
from numpy import ceil, floor


def float_round(num, places=2, direction=ceil):
    return direction(num * (10 ** places)) / float(10 ** places)


df = pd.read_csv('./data/titanic.csv')
df['age'] = df['age'].map(float_round)
df.tail(5)[['survived', 'pclass', 'sex', 'age']]
     survived  pclass     sex   age
886         0       2    male  27.0
887         1       1  female  19.0
888         0       3  female   NaN  # Still NaN no errors
889         1       1    male  26.0
890         0       3    male  32.0

Otherwise check if np.isnan before trying to call math.ceil or math.floor:

from math import ceil, floor

import numpy as np
import pandas as pd


def float_round(num, places=2, direction=ceil):
    if not np.isnan(num):
        return direction(num * (10 ** places)) / float(10 ** places)
    else:
        return np.NaN
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57