Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does `df.astype('int')` change the value of the number?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
>>>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading