Skip to content

BUG: Period datatype data gets mangled up in pivoting operation #61338

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
RobertasA opened this issue Apr 22, 2025 · 1 comment
Closed
2 of 3 tasks

BUG: Period datatype data gets mangled up in pivoting operation #61338

RobertasA opened this issue Apr 22, 2025 · 1 comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@RobertasA
Copy link

RobertasA commented Apr 22, 2025

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
df = pd.DataFrame({
    "id1": [1, 2], 
    "id2": [10, 20], 
    "id3": [100, 200], 
    "period":[pd.Period("2021-01"), pd.Period("2021-03")]
}).set_index(['id1','id2'])
result = df.unstack().stack(future_stack=True)

#fails - unexpected
assert (result.loc[df.index]==df).all().all()

Issue Description

The data in the "period" column gets mangled up, the value associated with the first record shows up twice and the value of the second record disappears.
The problem appears with both future_stack=True and future_stack=False.
The problem does not appear when stacking "period" series, only when stacking dataframe (so following unstack(), the columns are a mutliindex).

Expected Behavior

It is expected that df.unstack().stack() would return the original records unchanged.
Changing period dtype to 'str' behaves as expected:

import pandas as pd
df = pd.DataFrame({
    "id1": [1, 2], 
    "id2": [10, 20], 
    "id3": [100, 200], 
    "period":[pd.Period("2021-01"), pd.Period("2021-03")]
}).set_index(['id1','id2'])

#succeeds - expected
df2 = df.astype({"period": "str"})
result = df2.unstack().stack(future_stack=True)
assert (result.loc[df2.index]==df2).all().all()

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.11.11
python-bits : 64
OS : Linux
OS-release : 5.10.226-214.880.amzn2.x86_64
Version : #1 SMP Tue Oct 8 16:18:15 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.3
numpy : 1.26.4
pytz : 2025.1
dateutil : 2.9.0.post0
pip : 25.0.1
Cython : None
sphinx : None
IPython : 9.0.2
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2025.3.0
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : 3.10.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 19.0.1
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : 2025.3.0
scipy : 1.15.2
sqlalchemy : 2.0.39
tables : None
tabulate : 0.9.0
xarray : 2025.1.2
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.1
qtpy : None
pyqt5 : None

@RobertasA RobertasA added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 22, 2025
@RobertasA
Copy link
Author

After further investigation I can see that the bug is actually in unstack and is duplicate of #60980, already fixed in main but not released yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

1 participant