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 numpy as np
>>>import pandas as pd
>>>idx = pd.IndexSlice
>>>df = pd.DataFrame(np.random.rand(8, 4),
index=pd.MultiIndex.from_product([["A", "B"], range(2), ['a', 'b']]),
columns=pd.MultiIndex.from_product([['a', 'b'], range(2)]))
>>>df
a b
0 1 0 1
A 0 a 0.791022 0.810915 0.903327 0.623326
b 0.637121 0.521150 0.036788 0.302978
1 a 0.491420 0.622780 0.889818 0.859360
b 0.212784 0.056860 0.562042 0.349412
B 0 a 0.246708 0.522429 0.822344 0.134595
b 0.219090 0.909012 0.166682 0.601046
1 a 0.638349 0.458048 0.997902 0.152901
b 0.294982 0.435510 0.350015 0.961403
>>>df.loc["A"] # Drops first level, since selecting with scalar value
a b
0 1 0 1
0 a 0.791022 0.810915 0.903327 0.623326
b 0.637121 0.521150 0.036788 0.302978
1 a 0.491420 0.622780 0.889818 0.859360
b 0.212784 0.056860 0.562042 0.349412
>>>df.loc[idx["A", :, :], :] # Keeps first level, despite selecting with scalar value in first level
a b
0 1 0 1
A 0 a 0.791022 0.810915 0.903327 0.623326
b 0.637121 0.521150 0.036788 0.302978
1 a 0.491420 0.622780 0.889818 0.859360
b 0.212784 0.056860 0.562042 0.349412
>>>df.loc[idx["A"]] # Not how I would normally slice, but it is within the realm of possibilities. Scalar value drops level
a b
0 1 0 1
0 a 0.791022 0.810915 0.903327 0.623326
b 0.637121 0.521150 0.036788 0.302978
1 a 0.491420 0.622780 0.889818 0.859360
b 0.212784 0.056860 0.562042 0.349412
>>>df.loc[idx["A", :, :]] # Slicing on 2 out of 3 levels will keep the level
a b
0 1 0 1
A 0 a 0.791022 0.810915 0.903327 0.623326
b 0.637121 0.521150 0.036788 0.302978
1 a 0.491420 0.622780 0.889818 0.859360
b 0.212784 0.056860 0.562042 0.349412
>>>df.loc[idx["A", :]] # Slicing on 1 level will drop the level
a b
0 1 0 1
0 a 0.791022 0.810915 0.903327 0.623326
b 0.637121 0.521150 0.036788 0.302978
1 a 0.491420 0.622780 0.889818 0.859360
b 0.212784 0.056860 0.562042 0.349412
Issue Description
Not 100% sure this issue has not been reported yet. #10521 seems to discuss the same issue, but in my opinion doesn't generalize the problem well enough.
When selecting from the DataFrame using .loc and a scalar value, the level that scalar value is indexing is dropped. But when selecting the same using IndexSlice, the behaviour is unpredictable (see examples). Depending on which and how much levels are 'sliced' using a scalar, the level of that scalar selection may be dropped, but it is unclear what the rules for this are. Especially since there are some mentions in the documentation and other issues of scalars always dropping the level.
Expected Behavior
Any index level that is selected using a scalar value from that level should either have the level dropped across index selection tools, or not have the level dropped across index selection tools. Either way, it should be consistent.
Installed Versions
INSTALLED VERSIONS
commit : e8093ba
python : 3.10.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 13, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Netherlands.1252
pandas : 1.4.3
numpy : 1.23.0
pytz : 2022.1
dateutil : 2.8.2
setuptools : 63.1.0
pip : 22.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 : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : 2.1.1
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None