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
.