BUG: PythonParser does not use decimal separator when usecols and parse_date are specified · Issue #35873 · pandas-dev/pandas (original) (raw)


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

Repl: https://repl.it/repls/TiredLightcyanNetworking#main.py

import pandas as pd import io

data = io.StringIO('"dump","-9,1",20101010')

read_options = { 'names': ['col', 'col1', 'col2'], 'usecols': ['col1', 'col2'], 'parse_dates': ["col2"], 'decimal': ",", }

data.seek(0) c_engine = pd.read_csv( data, engine="c", **read_options, )

print(c_engine['col1'].dtype) # float64

data.seek(0) python_engine = pd.read_csv( data, engine="python", **read_options, )

print(python_engine['col1'].dtype) # object instead of float64

pd.testing.assert_frame_equal(c_engine, python_engine)

Raises

Attribute "dtype" are different

[left]: float64

[right]: object

Problem description

The PythonParser produces an invalid result when using a combination of options. It does not convert the comma correctly when the options 'usecols' and 'decimal' are used. The C engine does produce a correct result.

It seems that this check is wrong, the columns are shifted:
https://github.com/pandas-dev/pandas/blob/v1.1.1/pandas/io/parsers.py#L3089

The columns specified in that list are adjusted for the 'usecols' option (so shifted by 1 in the example), while the read line still has all columns.

Expected Output

Expect that decimals are correctly converted for all columns. However, the python engine does not when there are columns specified for date parsing.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2ca0a2
python : 3.8.3.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-1019-gcp
Version : #19-Ubuntu SMP Tue Jun 23 15:46:40 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.1
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 47.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.0
sqlalchemy : 1.3.17
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None