-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Unexpected KeyError message when using .loc with MultiIndex in a possible edge-case #51892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
take |
I am close to finding a root cause for this issue, I will update here in a while. |
import pandas as pd
df = pd.DataFrame({(1,2): ['a', 'b', 'c'],
(1,3): ['d', 'e', 'f'],
(2,2): ['g', 'h', 'i'],
(2,4): ['j', 'k', 'l']})
mi = df.axes[1]
print(mi.get_loc((1, 4))) Internally Traceback (most recent call last):
File "/home/user/code/pandas/test.py", line 8, in <module>
print(mi.get_loc((2, 3)))
File "/home/user/code/pandas/pandas/core/indexes/multi.py", line 2812, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 842, in pandas._libs.index.BaseMultiIndexCodesEngine.get_loc
return self._base.get_loc(self, lab_int)
File "pandas/_libs/index.pyx", line 147, in pandas._libs.index.IndexEngine.get_loc
cpdef get_loc(self, object val):
File "pandas/_libs/index.pyx", line 176, in pandas._libs.index.IndexEngine.get_loc
return self.mapping.get_item(val)
File "pandas/_libs/hashtable_class_helper.pxi", line 2152, in pandas._libs.hashtable.UInt64HashTable.get_item
cpdef get_item(self, uint64_t val):
File "pandas/_libs/hashtable_class_helper.pxi", line 2176, in pandas._libs.hashtable.UInt64HashTable.get_item
raise KeyError(val)
KeyError: 27 Now if we call Traceback (most recent call last):
File "/home/user/code/pandas/pandas/core/indexes/multi.py", line 2978, in _get_loc_level
return (self._engine.get_loc(key), None)
File "pandas/_libs/index.pyx", line 842, in pandas._libs.index.BaseMultiIndexCodesEngine.get_loc
return self._base.get_loc(self, lab_int)
File "pandas/_libs/index.pyx", line 147, in pandas._libs.index.IndexEngine.get_loc
cpdef get_loc(self, object val):
File "pandas/_libs/index.pyx", line 176, in pandas._libs.index.IndexEngine.get_loc
return self.mapping.get_item(val)
File "pandas/_libs/hashtable_class_helper.pxi", line 2152, in pandas._libs.hashtable.UInt64HashTable.get_item
cpdef get_item(self, uint64_t val):
File "pandas/_libs/hashtable_class_helper.pxi", line 2176, in pandas._libs.hashtable.UInt64HashTable.get_item
raise KeyError(val)
KeyError: 27
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/user/code/pandas/test.py", line 8, in <module>
print(mi.get_loc_level((2, 3)))
File "/home/user/code/pandas/pandas/core/indexes/multi.py", line 2909, in get_loc_level
loc, mi = self._get_loc_level(key, level=level)
File "/home/user/code/pandas/pandas/core/indexes/multi.py", line 2980, in _get_loc_level
raise KeyError(key) from err
KeyError: (2, 3) Both these methods internally make a call pandas/pandas/core/indexes/multi.py Lines 2811 to 2816 in 52c653b
pandas/pandas/core/indexes/multi.py Lines 2975 to 2982 in 52c653b
except, the second method has it wrapped in an except block which reraises the exception |
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
This code produces a KeyError with an incorrect message (aka incorrect key tested)
FWIW: this is a similarly wrong key but notice the different integer in the KeyError
KeyError: (1, 4)
The text was updated successfully, but these errors were encountered: