Skip to content

BUG: to_csv date_format is not taken into account for PeriodArrays #51621

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

Open
3 tasks done
smarie opened this issue Feb 24, 2023 · 1 comment
Open
3 tasks done

BUG: to_csv date_format is not taken into account for PeriodArrays #51621

smarie opened this issue Feb 24, 2023 · 1 comment
Assignees
Labels
Bug IO CSV read_csv, to_csv

Comments

@smarie
Copy link
Contributor

smarie commented Feb 24, 2023

First reported in #51459

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
>>> data = pd.DataFrame(pd.period_range(start="2000-01-01", periods=1, freq="H"))
>>> print(data.to_csv())
,0
0,2000-01-01 00:00
1,2000-01-01 01:00

>>> print(data.to_csv(date_format="%Y-%m-%d___%H:%M:%S"))
,0
0,2000-01-01 00:00
1,2000-01-01 01:00

>>> print(data.set_index(0).to_csv(date_format="%Y-%m-%d___%H:%M:%S"))
0
2000-01-01___00:00:00
2000-01-01___01:00:00

Issue Description

to_csv does not take into account date_format for PeriodArrays that are normal columns the dataframe. Only PeriodIndex are correctly formatted.

Expected Behavior

The formatting should be right even if the period array is not the index

Installed Versions

INSTALLED VERSIONS ------------------ commit : 705ff81 python : 3.8.13.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.19044 machine : AMD64 processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : fr_FR.cp1252 pandas : 1.5.0.dev0+3039.g705ff813d9.dirty numpy : 1.23.4 pytz : 2022.5 dateutil : 2.8.2 setuptools : 65.5.0 pip : 22.3 Cython : 0.29.32 pytest : 7.2.0 hypothesis : 6.56.4 sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : None IPython : None pandas_datareader: None bs4 : None bottleneck : None brotli : fastparquet : None fsspec : None gcsfs : None matplotlib : None numba : None numexpr : 2.8.3 odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None pyxlsb : None s3fs : None scipy : None snappy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None zstandard : None tzdata : None qtpy : None pyqt5 : None
@smarie smarie added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 24, 2023
@smarie smarie changed the title BUG: BUG: to_csv date_format is not taken into account for PeriodArrays Feb 24, 2023
@phofl phofl added IO CSV read_csv, to_csv and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 25, 2023
@lia2710
Copy link
Contributor

lia2710 commented Feb 28, 2023

take

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
Development

Successfully merging a pull request may close this issue.

3 participants