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: Inconsistent output type in Excel for PeriodIndex in Index or MultiIndex #60099

Open
3 tasks done
pforero opened this issue Oct 24, 2024 · 2 comments · May be fixed by #60182
Open
3 tasks done

BUG: Inconsistent output type in Excel for PeriodIndex in Index or MultiIndex #60099

pforero opened this issue Oct 24, 2024 · 2 comments · May be fixed by #60182
Labels
Bug IO Excel read_excel, to_excel MultiIndex Period Period data type

Comments

@pforero
Copy link

pforero commented Oct 24, 2024

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

period_index = pd.period_range(start="2006-10-06", end="2006-10-07", freq="D")
pi_index = pd.Series(0, index=period_index)
pi_multi = pd.Series(0, index=pd.MultiIndex.from_arrays([period_index, [0, 0]]))

pi_index.to_excel("index_as_date_openpyxl.xlsx", engine="openpyxl", index=True)
pi_multi.to_excel("index_as_str_openpyxl.xlsx", engine="openpyxl", index=True)

pi_index.to_excel("index_as_date_xlsxwriter.xlsx", engine="xlsxwriter",  index=True)
pi_multi.to_excel("index_as_str_xlsxwriter.xlsx", engine="xlsxwriter", index=True)

Issue Description

When exporting a Series/DataFrame to excel when the index contains a PeriodIndex the format of the output depends on the index being an Index or a MultiIndex.

When the PeriodIndex is in an Index, the output format in Excel is a date.
When the PeriodIndex is a level from a MultiIndex, the output format is a string.

Expected Behavior

The expected behaviour is that the format of the period would not be dependent on it being in an Index or in a MultiIndex. Being either a string or a date in excel is both acceptable as long as it is consistent in both outputs.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.12.4
python-bits : 64
OS : Linux
OS-release : 6.1.100+
Version : #1 SMP PREEMPT_DYNAMIC Sat Oct 5 14:28:44 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : None
LOCALE : C.UTF-8

pandas : 2.2.3
numpy : 2.0.0
pytz : 2024.1
dateutil : 2.9.0.post0
pip : 24.2
Cython : None
sphinx : None
IPython : 8.25.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 16.1.0
pyreadstat : None
pytest : 8.2.2
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 2.0.31
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : 3.2.0
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@pforero pforero added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 24, 2024
@ZKaoChi
Copy link
Contributor

ZKaoChi commented Oct 30, 2024

I think this is worth changing, can I try it ?

@rhshadrach
Copy link
Member

Thanks for the report. For a non-MultiIndex, we explicitly convert these to timestamps here:

if isinstance(self.df.index, PeriodIndex):
index_values = self.df.index.to_timestamp()

We should do something similar in the MultiIndex case. PRs are welcome!

@rhshadrach rhshadrach added IO Excel read_excel, to_excel Period Period data type MultiIndex and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO Excel read_excel, to_excel MultiIndex Period Period data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants