Skip to content

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

Open
@smarie

Description

@smarie

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions