Description
-
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 master branch of pandas.
Reproducible Example
>>> import pandas as pd
>>> df = pd.DataFrame({('A', 'a') : [1], ('A', 'b') : [2], ('B', 'c') : [3], ('B', 'd') : [4]})
>>> df
A B
a b c d
0 1 2 3 4
>>> df[('A', 'c')]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/site-packages/pandas/core/frame.py", line 3457, in __getitem__
return self._getitem_multilevel(key)
File "/usr/local/lib/python3.8/site-packages/pandas/core/frame.py", line 3508, in _getitem_multilevel
loc = self.columns.get_loc(key)
File "/usr/local/lib/python3.8/site-packages/pandas/core/indexes/multi.py", line 2932, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 725, in pandas._libs.index.BaseMultiIndexCodesEngine.get_loc
File "pandas/_libs/index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1832, in pandas._libs.hashtable.UInt64HashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1841, in pandas._libs.hashtable.UInt64HashTable.get_item
KeyError: 11
Issue Description
The example throws KeyError with mysterious number 11
instead of specified keys ('A', 'c')
.
It seems that multi-level index throws:
KeyError <keys>
if some of keys are invalid in their level (OK)KeyError <number>
if keys are valid for their level, but the combination is wrong (??)
I think this behavior is because of the lack of catch
for self._base.get_loc
:
Lines 711 to 725 in 945c9ed
Expected Behavior
It should throw KeyError: ('A', 'c')
even if all keys are valid for their levels.
Installed Versions
INSTALLED VERSIONS
commit : 945c9ed
python : 3.8.12.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.47-linuxkit
Version : #1 SMP Sat Jul 3 21:51:47 UTC 2021
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.3.4
numpy : 1.21.3
pytz : 2021.3
dateutil : 2.8.2
pip : 21.2.4
setuptools : 57.5.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None