Skip to content

BUG: remove_unused_levels does not keep index levels order #61245

Open
@mathman79

Description

@mathman79

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

df = pd.DataFrame(
    [
        ("aap", "1991-01-02", 100.0000),
        ("aap", "2024-12-24", 75.7575),
        ("noot", "1960-01-04", 11.111),
        ("noot", "2024-12-24", 123.45),
        ("noot", "2024-12-30", 321.54),
    ],
    columns=["name", "date", "value"],
).set_index(["name", "date"])["value"]

index = df.iloc[:-1].copy().index
assert all(index.levels[-1] == sorted(index.levels[-1]))

index2 = index.remove_unused_levels()
assert all(index2.levels[-1] == sorted(index2.levels[-1]))

Issue Description

Order or the multi index level is not kept. This causes issues with code like unstack being mis-ordered:

import pandas as pd

df = pd.DataFrame(
    [
        ("aap", "1991-01-02", 100.0000),
        ("aap", "2024-12-24", 75.7575),
        ("noot", "1960-01-04", 11.111),
        ("noot", "2024-12-24", 123.45),
        ("noot", "2024-12-30", 321.54),
    ],
    columns=["name", "date", "value"],
).set_index(["name", "date"])["value"]

df.iloc[:-1].unstack(level=0)

Expected Behavior

I expect that the current order or the multi index level is kept.

Installed Versions

INSTALLED VERSIONS

commit : f538741
python : 3.9.13.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 85 Stepping 7, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : English_United States.1252

pandas : 2.2.0
numpy : 1.26.4
pytz : 2023.3
dateutil : 2.8.2
setuptools : 63.4.1
pip : 24.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.8.0
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.7.0
pandas_datareader : 0.10.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.2
bottleneck : 1.3.7
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.10.0
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.8.8
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : 2024.10.0
scipy : 1.13.1
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : 2.0.1
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugMultiIndexNeeds DiscussionRequires discussion from core team before further actionSortinge.g. sort_index, sort_values

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions