Skip to content
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

Index out of bounds error raised for negative indices. #3168

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
4 changes: 2 additions & 2 deletions pandas/src/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ cdef inline object get_value_at(ndarray arr, object loc):
i = <Py_ssize_t> loc
sz = cnp.PyArray_SIZE(arr)

if i < 0 and sz > 0:
if i < 0 and -sz < i and sz > 0:
i += sz
elif i >= sz or sz == 0:
elif i >= sz or i < -sz:
raise IndexError('index out of bounds')

return get_value_1d(arr, i)
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,9 @@ def test_getitem_out_of_bounds(self):
s = Series([])
self.assertRaises(IndexError, s.__getitem__, -1)

# GH #
self.assertRaises(IndexError, s.__getitem__, - len(self.ts) - 1)

def test_getitem_setitem_integers(self):
# caused bug without test
s = Series([1, 2, 3], ['a', 'b', 'c'])
Expand Down