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: to_csv produces improper output for categorical datetimes #40754

Closed
2 of 3 tasks
qwertystop opened this issue Apr 2, 2021 · 3 comments · Fixed by #44930
Closed
2 of 3 tasks

BUG: to_csv produces improper output for categorical datetimes #40754

qwertystop opened this issue Apr 2, 2021 · 3 comments · Fixed by #44930
Assignees
Labels
Bug Categorical Categorical Data Type IO CSV read_csv, to_csv
Milestone

Comments

@qwertystop
Copy link

  • 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

>>> ser = pd.to_datetime(pd.Series(["2021-03-27"]), format="%Y-%m-%d")
>>> ser
0   2021-03-27
dtype: datetime64[ns]
>>> ser.to_csv()
',0\n0,2021-03-27\n'
>>> ser.astype("category")
0   2021-03-27
dtype: category
Categories (1, datetime64[ns]): [2021-03-27]
>>> ser.astype("category").to_csv()
',0\n0,1616803200000000000\n'
>>> ser.astype("category").to_csv(date_format="%Y-%m-%dT%H:%M:%S")
',0\n0,1616803200000000000\n'

Problem description

When a categorical datetime is written as CSV, it disregards date_format (even if given explicitly) and writes the timestamp in integer nanoseconds.

Expected Output

>>> ser.astype("category").to_csv()
',0\n0,2021-03-27\n'
>>> ser.astype("category").to_csv(date_format="%Y-%m-%dT%H:%M:%S")
',0\n0,2021-03-27T00:00:00\n'

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.9.1.final.0
python-bits : 64
OS : Linux
OS-release : 5.11.8-arch1-1
Version : #1 SMP PREEMPT Sun, 21 Mar 2021 01:55:51 +0000
machine : x86_64
processor :
byteorder : little
LC_ALL :
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.3
numpy : 1.19.5
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 49.2.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.20.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 0.8.7
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@qwertystop qwertystop added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 2, 2021
@rhshadrach
Copy link
Member

Thanks for the report @qwertystop! Confirmed on master, investigations and PR to fix are welcome.

@rhshadrach rhshadrach added Categorical Categorical Data Type IO CSV read_csv, to_csv and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 6, 2021
@rhshadrach rhshadrach added this to the Contributions Welcome milestone Apr 6, 2021
@kyleolaughlin
Copy link

take

@AndrewEckart
Copy link
Contributor

AndrewEckart commented Jun 23, 2021

Temporary workaround is to cast the categories to strings using .strftime and then cast back after .to_csv:

>>> import pandas as pd
>>> fmt = "%Y-%m-%d"
>>> ser = pd.to_datetime(pd.Series(["2021-03-27"]), format=fmt).astype("category")
>>> ser
0   2021-03-27
dtype: category
Categories (1, datetime64[ns]): [2021-03-27]
>>> ser.cat.rename_categories(ser.cat.categories.strftime(fmt), inplace=True)
>>> ser.to_csv()
',0\n0,2021-03-27\n'
>>> ser.cat.rename_categories(pd.to_datetime(ser.cat.categories, format=fmt), inplace=True)
>>> ser
0   2021-03-27
dtype: category
Categories (1, datetime64[ns]): [2021-03-27]

@jreback jreback modified the milestones: Contributions Welcome, 1.4 Dec 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type IO CSV read_csv, to_csv
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants