Skip to content

Commit 02ca451

Browse files
committed
Correct out-of-bounds error with large indeces
1 parent 26782a9 commit 02ca451

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pandas/index.pyx

+2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ cdef class IndexEngine:
143143
return self._get_loc_duplicates(val)
144144
values = self._get_index_values()
145145
loc = _bin_search(values, val) # .searchsorted(val, side='left')
146+
if loc >= len(values):
147+
raise KeyError(val)
146148
if util.get_value_at(values, loc) != val:
147149
raise KeyError(val)
148150
return loc

pandas/tests/indexes/test_multi.py

+17
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,23 @@ def test_equals_operator(self):
20522052
# GH9785
20532053
self.assertTrue((self.index == self.index).all())
20542054

2055+
def test_large_multiindex_error(self):
2056+
# GH12527
2057+
df_below_1000000 = pd.DataFrame(
2058+
1, index=pd.MultiIndex.from_product([[1, 2], range(499999)]),
2059+
columns=['dest'])
2060+
with assertRaises(KeyError):
2061+
df_below_1000000.loc[(-1, 0), 'dest']
2062+
with assertRaises(KeyError):
2063+
df_below_1000000.loc[(3, 0), 'dest']
2064+
df_above_1000000 = pd.DataFrame(
2065+
1, index=pd.MultiIndex.from_product([[1, 2], range(500001)]),
2066+
columns=['dest'])
2067+
with assertRaises(KeyError):
2068+
df_above_1000000.loc[(-1, 0), 'dest']
2069+
with assertRaises(KeyError):
2070+
df_above_1000000.loc[(3, 0), 'dest']
2071+
20552072
def test_partial_string_timestamp_multiindex(self):
20562073
# GH10331
20572074
dr = pd.date_range('2016-01-01', '2016-01-03', freq='12H')

0 commit comments

Comments
 (0)