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
import pandas as pd
ind = pd.MultiIndex.from_tuples([(0, 'a', 'B'),
(0, 'a', 'A'),
(0, 'b', 'B'),
(0, 'b', 'A'),
(1, 'a', 'B'),
(1, 'a', 'A'),
(1, 'b', 'B'),
(1, 'b', 'A'),
], names=['sample', 'cat0', 'cat1'])
ds = pd.Series(range(8), index=ind)
print(ds)
s01 = ds.groupby(level=['cat0', 'cat1'], sort=False).quantile([0.2, 0.8])
print('\n(cat0, cat1) Quantiles As Series: Result as expected:\n', s01)
d01 = s01.unstack()
print('\nQuantiles Unstacked: Result as expected:\n', d01)
s1 = ds.groupby(level='cat1', sort=False).quantile([0.2, 0.8])
print('\ncat1 Quantiles As Series: Result as expected:\n', s1)
d1 = s1.unstack()
print('\nQuantiles Unstacked: NOT as expected; cat1 index SORTED alphabetically:\n', d1)
print('\nQuantiles Unstacked, by iterrows:')
for (cat, q) in d1.iterrows():
print(f'{cat}: {q.values}')
Issue Description
I need to preserve the pre-defined order of MultiIndex labels in both 'cat0' and 'cat1', when calculating quantiles. The problem is that the result is as expected when the index 'cat1' in the example variable d01 is the second of two groupby levels, but does NOT give a similar expected result when 'cat1' is the single groupby level, as in the variable d1 in the example.
Expected Behavior
In my code example, I expected the unstacked DataFrame d1 to retain the index order ['B', 'A'], same as in the Series variable s1, and in analogy to the similarly unstack-ed DataFrame variable d01.
Installed Versions
INSTALLED VERSIONS
commit : 37ea63d
python : 3.10.7.final.0
python-bits : 64
OS : Darwin
OS-release : 22.4.0
Version : Darwin Kernel Version 22.4.0: Mon Mar 6 21:01:02 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T8112
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8
pandas : 2.0.1
numpy : 1.23.3
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 63.2.0
pip : 23.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
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.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None