Skip to content

Correct out-of-bounds error with large indeces #12921

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 1 commit 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
2 changes: 2 additions & 0 deletions pandas/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ cdef class IndexEngine:
return self._get_loc_duplicates(val)
values = self._get_index_values()
loc = _bin_search(values, val) # .searchsorted(val, side='left')
if loc >= len(values):
raise KeyError(val)
if util.get_value_at(values, loc) != val:
raise KeyError(val)
return loc
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/indexes/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,23 @@ def test_equals_operator(self):
# GH9785
self.assertTrue((self.index == self.index).all())

def test_large_multiindex_error(self):
# GH12527
df_below_1000000 = pd.DataFrame(
1, index=pd.MultiIndex.from_product([[1, 2], range(499999)]),
columns=['dest'])
with assertRaises(KeyError):
df_below_1000000.loc[(-1, 0), 'dest']
with assertRaises(KeyError):
df_below_1000000.loc[(3, 0), 'dest']
df_above_1000000 = pd.DataFrame(
1, index=pd.MultiIndex.from_product([[1, 2], range(500001)]),
columns=['dest'])
with assertRaises(KeyError):
df_above_1000000.loc[(-1, 0), 'dest']
with assertRaises(KeyError):
df_above_1000000.loc[(3, 0), 'dest']

def test_partial_string_timestamp_multiindex(self):
# GH10331
dr = pd.date_range('2016-01-01', '2016-01-03', freq='12H')
Expand Down