-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
Description
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
a = pd.Series([f'a{i}' for i in range(10)])
b = pd.Series([f'b{i}' for i in range(10)])
ser = pd.concat({'a': a, 'b': b}) # multi-indexed series e.g. ('a', 0) = 'a0'
df = ser.unstack(0, sort=False) # dataframe with integer index and columns=['a','b'], some a values end up in b column and vice-versa
Issue Description
When unstacking a multi-indexed series (or dataframe), passing sort=False fails to preserve the original mapping of multi-index keys to values. In other words, the resulting "a" column has a mix of "a" and "b"-prefixed values.
Expected Behavior
I would expect sort=False to prevent a sort of the newly produced column names but preserve the mapping of multi-index keys to values based on the following from the documentation: "sort: Sort the level(s) in the resulting MultiIndex columns."
Installed Versions
INSTALLED VERSIONS
commit : 0691c5c
python : 3.11.9
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 2.2.3
numpy : 2.2.2
pytz : 2025.1
dateutil : 2.9.0.post0
pip : 25.0.1
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