read_csv returns different float values for same number
See original GitHub issueCode Sample, a copy-pastable example if possible
test.csv
-15.361
-15.361000
>>> import pandas as pd
>>> x = pd.read_csv('test.csv', header=None)
>>> x.loc[0, 0] == x.loc[1, 0]
False
Problem description / Expected output
The expected output of the code above is
>>> x.loc[0, 0] == x.loc[1, 0]
True
We should expect both -15.361
and -15.361000
to be converted to the same np.float64
representation. However, they are converted to different float values, differing in exactly the last bit of their floating point representation. For some reason, -15.361
gets converted incorrectly to 0xC02EB8D4FDF3B645
whereas -15.361000
is correctly to 0xC02EB8D4FDF3B646
.
For completeness, here are some more comparisons
x.loc[1, 0]
is equal (==
) to np.float64('-15.361')
, np.float64('-15.361000')
, and float('-15.361000')
.
x.loc[0, 0]
is not equal to any of those.
Output of pd.show_versions()
[paste the output of pd.show_versions()
here below this line]
INSTALLED VERSIONS
commit: None python: 3.5.3.final.0 python-bits: 64 OS: Linux OS-release: 3.13.0-73-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8
pandas: 0.20.3 pytest: None pip: 9.0.1 setuptools: 36.2.4 Cython: None numpy: 1.13.1 scipy: 0.19.1 xarray: None IPython: None sphinx: None patsy: None dateutil: 2.6.1 pytz: 2017.2 blosc: None bottleneck: None tables: None numexpr: None feather: None matplotlib: 2.0.2 openpyxl: None xlrd: None xlwt: None xlsxwriter: None lxml: None bs4: None html5lib: 0.9999999 sqlalchemy: None pymysql: None psycopg2: None jinja2: None s3fs: None pandas_gbq: None pandas_datareader: None
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:11 (11 by maintainers)
Top GitHub Comments
Trying to get exact equality out of floating points is generally a losing battle, doubly so with a lossy format like csv - do use one of
float_precision
options if it’s important.Thanks @jreback! Glad this finally is resolved 👍