We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
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.
import pandas as pd series1 = pd.DataFrame( [(0, "s2", pd.Period(2022)), (0, "s1", pd.Period(2021))], columns=["A", "B", "C"] ).set_index(["A", "B"])["C"] series2 = series1.astype(str) print(series1.unstack("B").reindex(["s2"], axis=1)) print(series2.unstack("B").reindex(["s2"], axis=1))
The example code prints
B s2 A 0 2021
B s2 A 0 2022
Expect the result for both pd.Period and str data to be 2022:
(actually observed with older Pandas 2.0.3)
INSTALLED VERSIONS commit : 0691c5c python : 3.11.10 python-bits : 64 OS : Linux OS-release : 6.2.16 Version : #1 SMP PREEMPT_DYNAMIC Tue Jan 1 00:00:00 UTC 1980 machine : x86_64 processor : byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8 pandas : 2.2.3 numpy : 2.2.3 pytz : 2025.1 dateutil : 2.9.0.post0 pip : 24.0 Cython : None sphinx : None IPython : None 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 : None pandas_gbq : None psycopg2 : None pymysql : None pyarrow : None pyreadstat : None pytest : None python-calamine : None pyxlsb : None s3fs : None scipy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlsxwriter : None zstandard : None tzdata : 2025.1 qtpy : None pyqt5 : None
The text was updated successfully, but these errors were encountered:
Thanks for the report! Confirmed on main. There has to be some internal state that gets set wrong. Constructing the unstacked result directly:
series1 = pd.DataFrame( [(0, "s2", pd.Period(2022)), (0, "s1", pd.Period(2021))], columns=["A", "B", "C"] ).set_index(["A", "B"])["C"] unstacked = series1.unstack("B") df = pd.DataFrame({"s1": pd.Period(2021), "s2": pd.Period(2022)}, index=pd.Index([0], name="A")) df.columns.name = "B" tm.assert_frame_equal(unstacked, df)
does not exhibit the buggy behavior either when using reindex. Further investigations and PRs to fix are welcome!
reindex
Sorry, something went wrong.
@Pranav970 - why was this issue opened? It is identical to #60980.
Closing this as the other issue was opened first.
No branches or pull requests
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
Issue Description
The example code prints
B s2
A
0 2021
B s2
A
0 2022
Expected Behavior
Expect the result for both pd.Period and str data to be 2022:
B s2
A
0 2022
B s2
A
0 2022
(actually observed with older Pandas 2.0.3)
Installed Versions
INSTALLED VERSIONS
commit : 0691c5c
python : 3.11.10
python-bits : 64
OS : Linux
OS-release : 6.2.16
Version : #1 SMP PREEMPT_DYNAMIC Tue Jan 1 00:00:00 UTC 1980
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.3
numpy : 2.2.3
pytz : 2025.1
dateutil : 2.9.0.post0
pip : 24.0
Cython : None
sphinx : None
IPython : None
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 : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.1
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: