-
-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
BUG: MultiIndex level not sorted (as desired) after making it a CategoricalIndex #47607
Comments
can confirm that works as intended for regular Index and data columns. There is another issue where a level of a MultiIndex appears to be not sorted. Maybe related. see #21136 (comment) contributions and PRs to fix welcome. |
take |
When you use |
Sorry for the late reply. |
I've found another thing. Basically, the problem seems when the multi-index is already sorted in lexicographical order. If somehow that ordering is removed then the sorting works according to categorical index. Currently,
which doesn't work and the But if we remove the lexicographical sorting somehow, like below
and then sort
All thanks to this answer on stackoverflow. |
Just wasted a lot of time dealing with this bug. +1 to users who want this fixed. |
Any progress on this @GYHHAHA? I'm encountering this bug too and it's was very hard to pin down the issue. Fortunately this workaround worked for me as well:
|
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
I wanted to sort the months at MultiIndex level0 according to real life scenario. So I used
CategoricalIndex
and assigned it to the level0 of the MultiIndex. What I expected aftersort_index
was that the level0 would be sorted using the order defined in the CategoricalIndex.But even after making them as CategoricalIndex they are not sorted as expected.
Expected Behavior
What I wanted is that months should be sorted according to the given order in CategoricalIndex
The expected behaviour can be produced by this code as follows
Installed Versions
INSTALLED VERSIONS
commit : 4bfe3d0
python : 3.9.12.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 1.4.2
numpy : 1.21.5
pytz : 2022.1
dateutil : 2.8.2
pip : 21.2.4
setuptools : 61.2.0
Cython : 0.29.30
pytest : 7.1.2
hypothesis : None
sphinx : 4.4.0
blosc : None
feather : None
xlsxwriter : 3.0.3
lxml.etree : 4.8.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 8.3.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : 1.3.4
brotli :
fastparquet : None
fsspec : 2022.3.0
gcsfs : None
markupsafe : 2.0.1
matplotlib : 3.5.1
numba : 0.55.1
numexpr : 2.8.3
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
snappy :
sqlalchemy : 1.4.32
tables : 3.6.1
tabulate : 0.8.9
xarray : 0.20.1
xlrd : 2.0.1
xlwt : None
zstandard : None
The text was updated successfully, but these errors were encountered: