Skip to content

BUG: #10645 in using MultiIndex.__contains__ #10675

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,5 @@ Bug Fixes
- Bug in ``io.common.get_filepath_or_buffer`` which caused reading of valid S3 files to fail if the bucket also contained keys for which the user does not have read permission (:issue:`10604`)
- Bug in vectorised setting of timestamp columns with python ``datetime.date`` and numpy ``datetime64`` (:issue:`10408`, :issue:`10412`)

- Bug in ``MultiIndex.__contains__`` throws an ``IndexError`` for large multiindices (:issue:`10645`)
- Bug in ``pd.DataFrame`` when constructing an empty DataFrame with a string dtype (:issue:`9428`)
5 changes: 5 additions & 0 deletions pandas/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ cdef class IndexEngine:
return self._get_loc_duplicates(val)
values = self._get_index_values()
loc = _bin_search(values, val) # .searchsorted(val, side='left')

# GH10675
if len(values) <= loc or 0 > loc:
raise KeyError(val)

if util.get_value_at(values, loc) != val:
raise KeyError(val)
return loc
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,14 @@ def test_get_loc(self):
with tm.assertRaises(TypeError):
idx.get_loc('a', method='nearest')

def test_get_loc_keyerror(self):
# GH10645
mi = pd.MultiIndex.from_arrays([range(100), range(100)])
self.assertRaises(KeyError, lambda: mi.get_loc((1000001, 0)))

mi = pd.MultiIndex.from_arrays([range(1000000), range(1000000)])
self.assertRaises(KeyError, lambda: mi.get_loc((1000001, 0)))

def test_slice_locs(self):
for dtype in [int, float]:
idx = Index(np.array([0, 1, 2, 5, 6, 7, 9, 10], dtype=dtype))
Expand Down