BUG: pd.Series.unstack with mixed names types · Issue #48763 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

import pandas as pd

arrays = [["a", "a", "a", "a"], [1, 1, 2, 2], ["red", "blue", "red", "blue"]]

create multi-index with mixed name types

idx = pd.MultiIndex.from_arrays(arrays, names=("letter", 0, "color")) s = pd.Series([1,2,3,4], index=idx)

print(s.unstack('letter'))

output does not show the 'letter' level unstacked

#> 0 1 2 #> letter color #> a blue 2 4 #> red 1 3

change the name to a string

idx = pd.MultiIndex.from_arrays(arrays, names=("letter", "0", "color")) s = pd.Series([1,2,3,4], index=idx)

print(s.unstack('letter'))

output shows the correct level unstacked

#> letter a #> 0 color #> 1 blue 2 #> red 1 #> 2 blue 4 #> red 3

Issue Description

When calling pd.Series.unstack using a multi-index with mixed string and integer names, the incorrect level is unstacked when referencing a string level. When all of the levels are string, however, the correct level is unstacked.

Expected Behavior

pd.Series.unstack should always unstack the requested level regardless of the types of the level names in the multi-index

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e
python : 3.10.0.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 140 Stepping 1, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : English_United States.1252

pandas : 1.5.0
numpy : 1.23.3
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 57.4.0
pip : 21.2.3
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.0
numba : None
numexpr : None
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
xlwt : None
zstandard : None
tzdata : None