Pandas version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] 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


idx = pd.IndexSlice

index = pd.MultiIndex.from_tuples(
    [("a", "b", "c"), ("a", "a", "c"), ("a", "c", "b")],
    names=["col1", "col2", "col3"]
)

df = pd.DataFrame(index=index, data={"value": [1, 2, 3]})

# works and returns empty dataframe (for every key, that is not in the level "col2")
df.loc[idx[:, ["a"], ["somekey"]]]
# raises KeyError (for every key given for level "col2" that is available in that level, but not in the given combination of keys.
# If the above is itended (returning empty DataFrame), then this should return an empty DataFrame as well
df.loc[idx[:, ["a"], ["b"]]]

Issue Description

See reproducible example.

To fix that the lines

https://github.com/pandas-dev/pandas/blob/c35eca35d6070ea508a115ef1008ab40f33ed727/pandas/core/indexes/multi.py#L3290-L3291

should not raise an Exception but return

np.array([], dtype=np.intp)

like when an item cannot be found in the current index level:

https://github.com/pandas-dev/pandas/blob/c35eca35d6070ea508a115ef1008ab40f33ed727/pandas/core/indexes/multi.py#L3269-L3272

Expected Behavior

return an empty dataframe, if the combination of keys in the levels is not available.

>>> df.loc[idx[:, ["a"], ["b"]]]
Empty DataFrame
Columns: [value]
Index: []

Installed Versions

INSTALLED VERSIONS ------------------ commit : 8dab54d6573f7186ff0c3b6364d5e4dd635ff3e7 python : 3.9.13.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.19044 machine : AMD64 processor : Intel64 Family 6 Model 154 Stepping 3, GenuineIntel byteorder : little LC_ALL : None LANG : de_DE.UTF-8 LOCALE : de_DE.cp1252 pandas : 1.5.2 numpy : 1.23.2 pytz : 2022.2.1 dateutil : 2.8.2 setuptools : 65.3.0 pip : 22.2.2 Cython : None pytest : 7.1.2 hypothesis : None sphinx : 5.1.1 blosc : None feather : None xlsxwriter : 3.0.3 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 matplotlib : 3.5.3 numba : None numexpr : 2.8.3 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 : 3.7.0 tabulate : 0.8.10 xarray : 2022.6.0 xlrd : None xlwt : None zstandard : None tzdata : None

Comment From: fwitte

My bad, I confused the pandas installations in my environment and the clone repository. I guess #42351 dealt with it, although I'd have preferred the other way round :(.

Thanks and have a nice weekend!

Comment From: phofl

Closing then