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: PythonParser does not use decimal separator when usecols and parse_date are specified #35873

Closed
3 tasks done
cmulders opened this issue Aug 24, 2020 · 3 comments · Fixed by #38334
Closed
3 tasks done
Labels
Bug IO CSV read_csv, to_csv
Milestone

Comments

@cmulders
Copy link

cmulders commented Aug 24, 2020

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


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

@cmulders cmulders added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 24, 2020
@bashtage bashtage changed the title BUG: PythonParser does not convert decimals when usecols and parse_date are specified as well BUG: PythonParser does respect decimal separator usecols and parse_date are specified as well Aug 24, 2020
@bashtage bashtage added IO CSV read_csv, to_csv and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 24, 2020
@bashtage
Copy link
Contributor

The issue appears that decimal is ignored. For example,

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

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

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

@cmulders
Copy link
Author

cmulders commented Aug 24, 2020

Decimal is not ignored, otherwise the example below should not work.

Either 'engine', 'usecols', 'parse_dates' can be commented out, and both Series dtypes will be float64.

Very strange behaviour..

import pandas as pd
import io

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

python_engine = pd.read_csv(
  data,
  engine="python", # Comment out and both are float64
  names= ['col', 'col1', 'col2', 'col3'],
  usecols= ['col1', 'col2', 'col3'], # Comment out and both are float64
  parse_dates= ["col3"], # Comment out and both are float64
  decimal= ",",
)

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

@bashtage bashtage changed the title BUG: PythonParser does respect decimal separator usecols and parse_date are specified as well BUG: PythonParser does not use decimal separator when usecols and parse_date are specified Aug 24, 2020
@bashtage
Copy link
Contributor

When decimal="," then "9.1" should be a string (object) while "9,1" should be a float. In the testing I did above, it seemed like "9.1" was being converted to float even when decimal=",".

@jreback jreback modified the milestones: 1.2, Contributions Welcome Dec 7, 2020
@jreback jreback modified the milestones: Contributions Welcome, 1.3 Dec 14, 2020
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
3 participants