Skip to content

Commit c266dc3

Browse files
committed
Merge pull request #11049 from kawochen/BUG-FIX-10692
BUG: GH10645 and GH10692 where operation on large Index would error
2 parents 18f0fb3 + b8ed872 commit c266dc3

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

doc/source/whatsnew/v0.17.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ Bug Fixes
10331033
- Bug in ``Index.take`` may add unnecessary ``freq`` attribute (:issue:`10791`)
10341034
- Bug in ``merge`` with empty ``DataFrame`` may raise ``IndexError`` (:issue:`10824`)
10351035
- Bug in ``to_latex`` where unexpected keyword argument for some documented arguments (:issue:`10888`)
1036-
1036+
- Bug in indexing of large ``DataFrame`` where ``IndexError`` is uncaught (:issue:`10645` and :issue:`10692`)
10371037
- Bug in ``read_csv`` when using the ``nrows`` or ``chunksize`` parameters if file contains only a header line (:issue:`9535`)
10381038
- Bug in serialization of ``category`` types in HDF5 in presence of alternate encodings. (:issue:`10366`)
10391039
- Bug in ``pd.DataFrame`` when constructing an empty DataFrame with a string dtype (:issue:`9428`)

pandas/core/index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4727,7 +4727,7 @@ def __contains__(self, key):
47274727
try:
47284728
self.get_loc(key)
47294729
return True
4730-
except KeyError:
4730+
except LookupError:
47314731
return False
47324732

47334733
def __reduce__(self):

pandas/core/indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ def _convert_to_indexer(self, obj, axis=0, is_setter=False):
10401040
# if we are a label return me
10411041
try:
10421042
return labels.get_loc(obj)
1043-
except KeyError:
1043+
except LookupError:
10441044
if isinstance(obj, tuple) and isinstance(labels, MultiIndex):
10451045
if is_setter and len(obj) == labels.nlevels:
10461046
return {'key': obj}
@@ -1125,7 +1125,7 @@ def _convert_to_indexer(self, obj, axis=0, is_setter=False):
11251125
else:
11261126
try:
11271127
return labels.get_loc(obj)
1128-
except KeyError:
1128+
except LookupError:
11291129
# allow a not found key only if we are a setter
11301130
if not is_list_like_indexer(obj) and is_setter:
11311131
return {'key': obj}

pandas/tests/test_indexing.py

+10
Original file line numberDiff line numberDiff line change
@@ -4602,7 +4602,17 @@ def test_indexing_dtypes_on_empty(self):
46024602
assert_series_equal(df2.loc[:,'a'], df2.iloc[:,0])
46034603
assert_series_equal(df2.loc[:,'a'], df2.ix[:,0])
46044604

4605+
def test_large_dataframe_indexing(self):
4606+
#GH10692
4607+
result = DataFrame({'x': range(10**6)})
4608+
result.loc[len(result)] = len(result) + 1
4609+
expected = DataFrame({'x': range(10**6 + 1)})
4610+
assert_frame_equal(result, expected)
46054611

4612+
def test_large_mi_dataframe_indexing(self):
4613+
#GH10645
4614+
result = MultiIndex.from_arrays([range(10**6), range(10**6)])
4615+
assert(not (10**6, 0) in result)
46064616

46074617
class TestCategoricalIndex(tm.TestCase):
46084618

0 commit comments

Comments
 (0)