Humi
Humi

Reputation: 609

pandas dataframe float point issues

I have a column with float values such as 600.0750, 600.2274, 600.3798, 600.5322.I rounded the values to 3 decimal places and then applied the diff() function to that column which gave me values 0.1524,0.1524 and 0.1524.To remove the duplicates from the output, I applied drop_duplicates. I was expecting to see only 0.1524 but in actual I got 0.1524,0.1524,0.1524.

So I store the output values in another dataframe df_diff and I do df_diff.iloc[2] which gives me 0.15239999999994325 and for df_diff.iloc[3] I get 0.15240000000005693.

I go back and do the same for original df and I get 600.07500000000005. So even when I am rounding, the values are not getting rounded. What can I do to fix this?

Thanks

Upvotes: 1

Views: 1456

Answers (1)

Acccumulation
Acccumulation

Reputation: 3591

You say that you rounded to three decimal places, but your numbers have four. The following assumes that four is the correct number.

There are several options. First, you can try not rounding until right before you try to drop duplicates. If that doesn't work, if you aren't too particular about the exact level of precision you're rounding to, you can round to a power of 2, such as 2**14. A second option is to use the https://docs.python.org/2/library/fractions.html module. A third option is to multiply the numbers by 10000 and then round them to integers. If you want the original numbers back, then you can divide them by 10000 again.

Upvotes: 1

Related Questions