I was testing some code and found out about this quirk of pandas:
Let’s say you have a float number in your df, for example 57.99999999999999 but you need that number as an int, so you do df.astype('int'). The number you get is 57 (instead of 58).
Does anyone know why that happens?
Here’s some code to prove my point:
df = pd.DataFrame({'col1': [57.99999999999999]})
df2 = pd.DataFrame({'col1': [57.999999999999997]})
print(df.astype('int'))
print(df2.astype('int'))
I’ve noticed that while 57.99999999999999 and 57.999999999999996 get both converted to 57, 57.999999999999997 gets converted to 58.
>Solution :
That’s float inaccuracies, nothing specific to pandas or even python. float cannot save floating point numbers with arbitrary precision, see Is floating point math broken? As a quick test without pandas, see this output from an interactive python session
>>>l=57.999999999999997
>>> l
58.0
>>>