Skip to content

BUG: bug in recognizing out-of-bounds on iloc indexers on tuple indexers (GH ) #7189

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
May 21, 2014
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
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ Bug Fixes
- Bug in ``isnull`` when applied to 0-dimensional object arrays (:issue:`7176`)
- Bug in ``query``/``eval`` where global constants were not looked up correctly
(:issue:`7178`)
- Bug in recognizing out-of-bounds positional list indexers with ``iloc`` and a multi-axis tuple indexer (:issue:`7189`)

pandas 0.13.1
-------------
Expand Down
43 changes: 32 additions & 11 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,13 +1278,38 @@ def _has_valid_type(self, key, axis):
"an indexable as a mask")
return True

return (isinstance(key, slice) or
com.is_integer(key) or
_is_list_like(key))
if isinstance(key, slice):
return True
elif com.is_integer(key):
return self._is_valid_integer(key, axis)
elif (_is_list_like(key)):
return self._is_valid_list_like(key, axis)
return False

def _has_valid_setitem_indexer(self, indexer):
self._has_valid_positional_setitem_indexer(indexer)

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):
raise IndexError("single positional indexer is out-of-bounds")
return True


def _is_valid_list_like(self, key, axis):
# return a boolean if we are a valid list-like (e.g. that we dont' have out-of-bounds values)

# coerce the key to not exceed the maximum size of the index
arr = np.array(key)
ax = self.obj._get_axis(axis)
l = len(ax)
if len(arr) and (arr.max() >= l or arr.min() <= -l):
raise IndexError("positional indexers are out-of-bounds")

return True

def _getitem_tuple(self, tup):

self._has_valid_tuple(tup)
Expand Down Expand Up @@ -1339,14 +1364,10 @@ def _getitem_axis(self, key, axis=0, validate_iterable=False):
# a single integer or a list of integers
else:

ax = self.obj._get_axis(axis)
if _is_list_like(key):

# coerce the key to not exceed the maximum size of the index
arr = np.array(key)
l = len(ax)
if len(arr) and (arr.max() >= l or arr.min() <= -l):
raise IndexError("positional indexers are out-of-bounds")
# validate list bounds
self._is_valid_list_like(key, axis)

# force an actual list
key = list(key)
Expand All @@ -1358,8 +1379,8 @@ def _getitem_axis(self, key, axis=0, validate_iterable=False):
raise TypeError("Cannot index by location index with a "
"non-integer key")

if key > len(ax):
raise IndexError("single positional indexer is out-of-bounds")
# validate the location
self._is_valid_integer(key, axis)

return self._get_loc(key, axis=axis)

Expand Down
78 changes: 78 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,84 @@ def test_iloc_getitem_frame(self):
# trying to use a label
self.assertRaises(ValueError, df.iloc.__getitem__, tuple(['j','D']))

def test_iloc_getitem_panel(self):

# GH 7189
p = Panel(np.arange(4*3*2).reshape(4,3,2),
items=['A','B','C','D'],
major_axis=['a','b','c'],
minor_axis=['one','two'])

result = p.iloc[1]
expected = p.loc['B']
assert_frame_equal(result, expected)

result = p.iloc[1,1]
expected = p.loc['B','b']
assert_series_equal(result, expected)

result = p.iloc[1,1,1]
expected = p.loc['B','b','two']
self.assertEqual(result,expected)

# slice
result = p.iloc[1:3]
expected = p.loc[['B','C']]
assert_panel_equal(result, expected)

result = p.iloc[:,0:2]
expected = p.loc[:,['a','b']]
assert_panel_equal(result, expected)

# list of integers
result = p.iloc[[0,2]]
expected = p.loc[['A','C']]
assert_panel_equal(result, expected)

# neg indicies
result = p.iloc[[-1,1],[-1,1]]
expected = p.loc[['D','B'],['c','b']]
assert_panel_equal(result, expected)

# dups indicies
result = p.iloc[[-1,-1,1],[-1,1]]
expected = p.loc[['D','D','B'],['c','b']]
assert_panel_equal(result, expected)

# combined
result = p.iloc[0,[True,True],[0,1]]
expected = p.loc['A',['a','b'],['one','two']]
assert_frame_equal(result, expected)

# out-of-bounds exception
self.assertRaises(IndexError, p.iloc.__getitem__, tuple([10,5]))
def f():
p.iloc[0,[True,True],[0,1,2]]
self.assertRaises(IndexError, f)

# trying to use a label
self.assertRaises(ValueError, p.iloc.__getitem__, tuple(['j','D']))

# GH
p = Panel(np.random.rand(4,3,2), items=['A','B','C','D'], major_axis=['U','V','W'], minor_axis=['X','Y'])
expected = p['A']

result = p.iloc[0,:,:]
assert_frame_equal(result, expected)

result = p.iloc[0,[True,True,True],:]
assert_frame_equal(result, expected)

result = p.iloc[0,[True,True,True],[0,1]]
assert_frame_equal(result, expected)

def f():
p.iloc[0,[True,True,True],[0,1,2]]
self.assertRaises(IndexError, f)

def f():
p.iloc[0,[True,True,True],[2]]
self.assertRaises(IndexError, f)

def test_iloc_getitem_doc_issue(self):

Expand Down