0

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.

  • This isn't really Pandas-related: in plain Python, `int(57.99999999999999)` gives 57 and `int(57.999999999999997)` gives 58. – slothrop Jun 30 '23 at 11:29

1 Answers1

0

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
>>>
FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
  • Yeah, but without the last 7 it also is 58 (it took me like 1h to figure out that the value 58 in the debugger was not 58 but 57.999999999) – David Siret Marqués Jun 30 '23 at 11:32
  • I don't know what you mean, in your own question, you wrote that `57.999999999` gets (correctly) converted to 57 when converted to int. Conversion to int is always rounding down – FlyingTeller Jun 30 '23 at 11:36
  • OK, I retested that outside the program and seems to work right. It's a problem with floating point numbers. Seems more a problem with the VScode representation than with python... – David Siret Marqués Jun 30 '23 at 11:38