Skip to content

BUG: fix bounds for negative ints when using iloc (GH 10779) #10808

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

Merged
merged 1 commit into from
Aug 14, 2015
Merged
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 doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,5 @@ Bug Fixes
- Bug in ``DatetimeIndex.take`` and ``TimedeltaIndex.take`` may not raise ``IndexError`` against invalid index (:issue:`10295`)
- Bug in ``Series([np.nan]).astype('M8[ms]')``, which now returns ``Series([pd.NaT])`` (:issue:`10747`)
- Bug in ``PeriodIndex.order`` reset freq (:issue:`10295`)
- Bug in ``iloc`` allowing memory outside bounds of a Series to be accessed with negative integers (:issue:`10779`)
- Bug preventing access to the first index when using ``iloc`` with a list containing the appropriate negative integer (:issue:`10547`, :issue:`10779`)
5 changes: 3 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,8 @@ def _is_valid_integer(self, key, axis):
# return a boolean if we have a valid integer indexer

ax = self.obj._get_axis(axis)
if key > len(ax):
l = len(ax)
if key >= l or key < -l:
raise IndexError("single positional indexer is out-of-bounds")
return True

Expand All @@ -1400,7 +1401,7 @@ def _is_valid_list_like(self, key, axis):
arr = np.array(key)
ax = self.obj._get_axis(axis)
l = len(ax)
if len(arr) and (arr.max() >= l or arr.min() <= -l):
if len(arr) and (arr.max() >= l or arr.min() < -l):
raise IndexError("positional indexers are out-of-bounds")

return True
Expand Down
34 changes: 33 additions & 1 deletion pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ def test_iloc_exceeds_bounds(self):
df.iloc[30]
self.assertRaises(IndexError, lambda : df.iloc[-30])

# GH10779
# single positive/negative indexer exceeding Series bounds should raise an IndexError
with tm.assertRaisesRegexp(IndexError, 'single positional indexer is out-of-bounds'):
s.iloc[30]
self.assertRaises(IndexError, lambda : s.iloc[-30])

# slices are ok
result = df.iloc[:,4:10] # 0 < start < len < stop
expected = df.iloc[:,4:]
Expand Down Expand Up @@ -471,7 +477,6 @@ def check(result,expected):
self.assertRaises(IndexError, lambda : dfl.iloc[[4,5,6]])
self.assertRaises(IndexError, lambda : dfl.iloc[:,4])


def test_iloc_getitem_int(self):

# integer
Expand All @@ -497,6 +502,33 @@ def test_iloc_getitem_list_int(self):
self.check_result('array int', 'iloc', np.array([2]), 'ix', { 0 : [4], 1 : [6], 2: [8] }, typs = ['ints'])
self.check_result('array int', 'iloc', np.array([0,1,2]), 'indexer', [0,1,2], typs = ['labels','mixed','ts','floats','empty'], fails = IndexError)

def test_iloc_getitem_neg_int_can_reach_first_index(self):
# GH10547 and GH10779
# negative integers should be able to reach index 0
df = DataFrame({'A': [2, 3, 5], 'B': [7, 11, 13]})
s = df['A']

expected = df.iloc[0]
result = df.iloc[-3]
assert_series_equal(result, expected)

expected = df.iloc[[0]]
result = df.iloc[[-3]]
assert_frame_equal(result, expected)

expected = s.iloc[0]
result = s.iloc[-3]
self.assertEqual(result, expected)

expected = s.iloc[[0]]
result = s.iloc[[-3]]
assert_series_equal(result, expected)

# check the length 1 Series case highlighted in GH10547
expected = pd.Series(['a'], index=['A'])
result = expected.iloc[[-1]]
assert_series_equal(result, expected)

def test_iloc_getitem_dups(self):

# no dups in panel (bug?)
Expand Down