0

Let's consider the following simple csv file

end_beat
2.989583333
1.989583333
2.989583333

Pandas Approach

When I try to use pandas to read this csv file with the following code

import pandas as pd
df = pd.read_csv('debug.csv')
print(df['end_beat'][1])

I got

1.9895833330000001

Python csv library approach

However, when I use another method to read the data, everything is fine

import csv

with open('debug.csv') as f:
    df = csv.DictReader(f)
    next(df)['end_beat']
    print(next(df)['end_beat'])

1.989583333

Only the result returned by pandas has this problem, how to get rid of the extra zeros?

Raven Cheuk
  • 2,903
  • 4
  • 27
  • 54
  • @cs95 My question is how to fix it. The question you post is asking why. These are two completely different questions. – Raven Cheuk Jun 29 '19 at 14:17
  • Once you understand why it is happening you either forget about it or ask a more informed question. Do you want to round? Truncate? "Get rid of it" is broad. – cs95 Jun 29 '19 at 14:23
  • 1
    I know floating point arithmetic could cause this problem. If that post is asking `a=0.3` yields `a == 0.3 >> False`, then I agree that my post is a duplicate of that post. But there's no arithmetic involved in my question, it's just value copying here. I don't get why just copying the value could also cause this problem. – Raven Cheuk Jun 29 '19 at 14:36
  • In any case, you'll find [this helpful](https://stackoverflow.com/questions/36319851/how-to-truncate-floating-points-imprecision-in-python). If you don't want rounding, you can apply `math.trunc` – cs95 Jun 29 '19 at 14:38
  • There doesn't have to be arithmetic involved for floating point precision errors to occur -- the number just has to not have an exact binary representation. – cs95 Jun 29 '19 at 14:39

0 Answers0