Skip to content

BUG: arguments dtype and parse_dates of read_csv does not work properly #57024

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

Closed
2 of 3 tasks
godslayer-inferno opened this issue Jan 23, 2024 · 7 comments
Closed
2 of 3 tasks
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@godslayer-inferno
Copy link

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame([{'name': 'foo','date': pd.Timestamp('2024-01-23T00:00:00Z')},
                   {'name': 'bar','date': pd.Timestamp('2024-01-23T12:00:00Z')}])
df.to_csv('test.csv', index=False)

df_read = pd.read_csv('test.csv', dtype=str, parse_dates=['date'])
print(df_read.dtypes) # The dtype of column 'date' should be datetime-like, but we got 'object' instead

Issue Description

Reading a csv file and trying to specify both dtype and parse_dates will not parse the columns specified in parse_dates as dates, but strings instead

Expected Behavior

The code in the example should print:
name object
date datetime64[ns, UTC]
dtype: object

Installed Versions

INSTALLED VERSIONS

commit : f538741
python : 3.11.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22621
machine : AMD64
processor : Intel64 Family 6 Model 183 Stepping 1, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : Chinese (Simplified)_China.936

pandas : 2.2.0
numpy : 1.24.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.3
Cython : None
pytest : 7.4.0
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
html5lib : 1.1
pymysql : 1.0.2
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.15.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2023.9.2
gcsfs : None
matplotlib : 3.7.2
numba : 0.58.0
numexpr : 2.8.7
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.11.3
sqlalchemy : 2.0.22
tables : None
tabulate : 0.9.0
xarray : None
xlrd : 2.0.1
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@godslayer-inferno godslayer-inferno added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 23, 2024
@aidoskanapyanov
Copy link
Contributor

I confirm this on main branch

>>> import pandas as pd
>>> print(pd.__version__)
3.0.0.dev0+163.g7281475054.dirty
>>> df = pd.DataFrame([{'name': 'foo','date': pd.Timestamp('2024-01-23T00:00:00Z')},
        ...                    {'name': 'bar','date': pd.Timestamp('2024-01-23T12:00:00Z')}])
>>> df.to_csv('test.csv', index=False)
>>> df_read = pd.read_csv('test.csv', dtype=str, parse_dates=['date'])
>>> print(df_read.dtypes)
name    object
date    object
dtype: object

@MarcoGorelli
Copy link
Member

this looks like it's as-documented, you set a dtype for the entire dataframe and it was respected

@MarcoGorelli
Copy link
Member

you can call to_datetime after having read the csv file

closing then but thanks for the report

@philpep
Copy link

philpep commented Jan 24, 2024

this looks like it's as-documented, you set a dtype for the entire dataframe and it was respected

Sure but parse_dates was ignored. Doesn't this deserve at least a warning or a changelog notice ?

The behavior has changed between 2.1.4 and 2.2.0, also parsing the date seem different (datetime vs date ?)

import io

import pandas as pd

df = pd.read_csv(
    io.StringIO("date,value\n2020-09-01,3\n"), dtype=object, parse_dates=["date"]
)
print(df)
print(df.dtypes)

With pandas 2.1.4:

        date value
0 2020-09-01     3
date     datetime64[ns]
value            object
dtype: object

With pandas 2.2.0:

                  date value
0  2020-09-01 00:00:00     3
date     object
value    object
dtype: object

@MarcoGorelli
Copy link
Member

ah hadn't realised it was a regression, sorry - reopening then, running git bisect now

@MarcoGorelli MarcoGorelli reopened this Jan 24, 2024
@MarcoGorelli
Copy link
Member

this changed in #56047

@MarcoGorelli
Copy link
Member

I think this is correct to be honest, closing again then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

4 participants