Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: read_csv fails with float_precision="round_trip" and decimal="," #35365

Closed
2 tasks done
ales-erjavec opened this issue Jul 21, 2020 · 1 comment · Fixed by #35377
Closed
2 tasks done

BUG: read_csv fails with float_precision="round_trip" and decimal="," #35365

ales-erjavec opened this issue Jul 21, 2020 · 1 comment · Fixed by #35377
Labels
Bug IO CSV read_csv, to_csv
Milestone

Comments

@ales-erjavec
Copy link
Contributor

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.


Code Sample, a copy-pastable example

import io
import pandas as pd

content = """\
6,5;3,3
3,4;1,0
"""

pd.read_csv(io.StringIO(content), delimiter=";", header=None,
            dtype=float, decimal=",", float_precision="round_trip")

Problem description

read_csv fails if using float_precision="round_trip" and decimal="," (or any other decimal and thousands separator).

It raises an

Traceback (most recent call last):
  File "pandas/_libs/parsers.pyx", line 1152, in pandas._libs.parsers.TextReader._convert_tokens
TypeError: Cannot cast array from dtype('O') to dtype('float64') according to the rule 'safe'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
  File "/Users/aleserjavec/virtual/py37/lib/python3.7/site-packages/pandas/io/parsers.py", line 676, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/Users/aleserjavec/virtual/py37/lib/python3.7/site-packages/pandas/io/parsers.py", line 454, in _read
    data = parser.read(nrows)
  File "/Users/aleserjavec/virtual/py37/lib/python3.7/site-packages/pandas/io/parsers.py", line 1133, in read
    ret = self._engine.read(nrows)
  File "/Users/aleserjavec/virtual/py37/lib/python3.7/site-packages/pandas/io/parsers.py", line 2037, in read
    data = self._reader.read(nrows)
  File "pandas/_libs/parsers.pyx", line 860, in pandas._libs.parsers.TextReader.read
  File "pandas/_libs/parsers.pyx", line 875, in pandas._libs.parsers.TextReader._read_low_memory
  File "pandas/_libs/parsers.pyx", line 952, in pandas._libs.parsers.TextReader._read_rows
  File "pandas/_libs/parsers.pyx", line 1084, in pandas._libs.parsers.TextReader._convert_column_data
  File "pandas/_libs/parsers.pyx", line 1158, in pandas._libs.parsers.TextReader._convert_tokens
ValueError: could not convert string to float: '6,5'

Expected Output

     0    1
0  6.5  3.3
1  3.4  1.0

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.4.final.0
python-bits : 64
OS : Darwin
OS-release : 18.7.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.5
numpy : 1.16.2
pytz : 2018.3
dateutil : 2.6.1
pip : 20.1.1
setuptools : 39.0.1
Cython : 0.29.13
pytest : 3.7.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.1.5
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 6.2.1
pandas_datareader: None
bs4 : 4.6.0
bottleneck : 1.2.1
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.3.0
numexpr : 2.6.6
odfpy : None
openpyxl : 3.0.2
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 3.7.1
pyxlsb : None
s3fs : None
scipy : 1.1.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.1.0
xlwt : None
xlsxwriter : 1.1.5
numba : 0.41.0

@ales-erjavec ales-erjavec added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 21, 2020
@simonjayhawkins simonjayhawkins added IO CSV read_csv, to_csv and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 21, 2020
@WillAyd
Copy link
Member

WillAyd commented Jul 21, 2020

This might trace back to here

double r = PyOS_string_to_double(p, q, 0);

I don't know if / how PyOS_string_to_double can accept a custom separator but if you can make it work and want to push a PR would be welcome

ales-erjavec added a commit to ales-erjavec/pandas that referenced this issue Jul 22, 2020
In case of non c-locale decimal and tsep, copy and fixup the source
string before passing it to PyOS_string_to_double
ales-erjavec added a commit to ales-erjavec/pandas that referenced this issue Jul 22, 2020
In case of non c-locale decimal and tsep, copy and fixup the source
string before passing it to PyOS_string_to_double
ales-erjavec added a commit to ales-erjavec/pandas that referenced this issue Jul 22, 2020
In case of non c-locale decimal and tsep, copy and fixup the source
string before passing it to PyOS_string_to_double
ales-erjavec added a commit to ales-erjavec/pandas that referenced this issue Jul 22, 2020
In case of non c-locale decimal and tsep, copy and fixup the source
string before passing it to PyOS_string_to_double
ales-erjavec added a commit to ales-erjavec/pandas that referenced this issue Jul 23, 2020
In case of non c-locale decimal and tsep, copy and fixup the source
string before passing it to PyOS_string_to_double
ales-erjavec added a commit to ales-erjavec/pandas that referenced this issue Jul 23, 2020
In case of non c-locale decimal and tsep, copy and fixup the source
string before passing it to PyOS_string_to_double
ales-erjavec added a commit to ales-erjavec/pandas that referenced this issue Jul 27, 2020
In case of non c-locale decimal and tsep, copy and fixup the source
string before passing it to PyOS_string_to_double
@jreback jreback added this to the 1.2 milestone Aug 6, 2020
ales-erjavec added a commit to ales-erjavec/pandas that referenced this issue Aug 10, 2020
In case of non c-locale decimal and tsep, copy and fixup the source
string before passing it to PyOS_string_to_double
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO CSV read_csv, to_csv
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants