Skip to content

BUG: indexing a MultiIndex-ed DataFrame (only) with a scalar on a level and a slice on another doesn't drop a level #62926

@toobaz

Description

@toobaz

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

In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: df = pd.DataFrame(np.arange(20).reshape((4, 5))).set_index([0, 1, 2])

In [4]: df.loc[(slice(None), 1,), :].index
Out[4]: 
MultiIndex([(0, 1, 2)],
           names=[0, 1, 2])

In [5]: df[3].loc[(slice(None), 1,)].index
Out[5]: 
MultiIndex([(0, 2)],
           names=[0, 2])

Issue Description

This is basically #12827 for DataFrames, since it was fixed (quite ironically, only) for Series.

Expected Behavior

Out[4] should be the same as Out[5].

Compare to

In [6]: df.loc[(0,), :].index
Out[6]: 
MultiIndex([(1, 2)],
           names=[1, 2])

... which correctly drops a level.

Installed Versions

INSTALLED VERSIONS

commit : 9c8bc3e
python : 3.11.2
python-bits : 64
OS : Linux
OS-release : 6.10.11+bpo-amd64
Version : #1 SMP PREEMPT_DYNAMIC Debian 6.10.11-1~bpo12+1 (2024-10-03)
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : it_IT.UTF-8
LOCALE : it_IT.UTF-8

pandas : 2.3.3
numpy : 2.3.4
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 23.0.1
Cython : None
sphinx : None
IPython : 9.6.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.14.2
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.6
lxml.etree : None
matplotlib : 3.10.7
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.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesMultiIndex

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions