Skip to content

BUG: ValueError in read_csv when dtype='string' and parse_dates is present #34066

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
meownoid opened this issue May 8, 2020 · 4 comments · Fixed by #44694
Closed
2 of 3 tasks

BUG: ValueError in read_csv when dtype='string' and parse_dates is present #34066

meownoid opened this issue May 8, 2020 · 4 comments · Fixed by #44694
Labels
Bug Datetime Datetime data dtype IO CSV read_csv, to_csv Strings String extension data type and string data
Milestone

Comments

@meownoid
Copy link

meownoid commented May 8, 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

import pandas as pd
import datetime
df = pd.DataFrame({'a': [1], 'b': [datetime.datetime.now()]})
df.to_csv('test.csv', index=None)
pd.read_csv('test.csv', dtype='string', parse_dates=['b'])

Problem description

ValueError: not all elements from date_cols are numpy arrays

Expected Output

Data frame with all columns as strings except ones specified in parse_dates optional argument.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.1.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-22-generic
machine : x86_64
processor :
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : None.None

pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3
Cython : 0.29.15
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.0
html5lib : None
pymysql : None
psycopg2 : 2.8.4 (dt dec pq3 ext lo64)
jinja2 : 2.11.2
IPython : 7.13.0
pandas_datareader: None
bs4 : 4.9.0
bottleneck : 1.3.2
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.1.3
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.16.0
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.13
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : 0.48.0

@meownoid meownoid added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 8, 2020
mproszewska added a commit to mproszewska/pandas that referenced this issue May 23, 2020
@gfyoung gfyoung added IO CSV read_csv, to_csv Datetime Datetime data dtype and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 25, 2020
@simonjayhawkins
Copy link
Member

@meownoid The string Dtype was added in 1.0.0, see #27949. Can you check the versions used for the working case. I'm getting ...

>>> import pandas as pd
>>>
>>> print(pd.__version__)
0.25.3
>>> import datetime
>>>
>>> df = pd.DataFrame({"a": [1], "b": [datetime.datetime.now()]})
>>> df.to_csv("test.csv", index=None)
>>> pd.read_csv("test.csv", dtype="string", parse_dates=["b"])
Traceback (most recent call last):
...
TypeError: data type 'string' not understood

@meownoid
Copy link
Author

@simonjayhawkins Sorry for confusion, in 0.25.3 worked alternative version of this code with the dtype="str":

pd.read_csv("test.csv", dtype="str", parse_dates=["b"])

@simonjayhawkins
Copy link
Member

Thanks @meownoid for the clarification. will update title.

@simonjayhawkins simonjayhawkins changed the title BUG: ValueError in read_csv when dtype='string' and parse_dates is present (worked in 0.25) BUG: ValueError in read_csv when dtype='string' and parse_dates is present Jul 28, 2020
@simonjayhawkins simonjayhawkins added the Strings String extension data type and string data label Jul 28, 2020
@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Jul 28, 2020
@dperlman
Copy link

This bug still stands and the copy-paste-able example still works. If I get up the motivation I might jump in as a contributor and fix it. In the meanwhile, a workaround is to not use the "dtype" keyword. The default actions of pd.read_csv tend to work pretty well. If they don't, you can clean up the dtypes after reading.

@jreback jreback modified the milestones: Contributions Welcome, 1.4 Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype IO CSV read_csv, to_csv Strings String extension data type and string data
Projects
None yet
5 participants