Skip to content

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

Closed
@ales-erjavec

Description

@ales-erjavec
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions