Skip to content

BUG: Cleaner exception when .iloc called with non-integer list #25759

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 11 commits into from
Mar 26, 2019
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ Interval
Indexing
^^^^^^^^

-
- Added an exception message when calling :meth:`DataFrame.iloc` with a list of non-numeric objects.
-
-

Expand Down
4 changes: 4 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2079,6 +2079,10 @@ def _validate_key(self, key, axis):
arr = np.array(key)
len_axis = len(self.obj._get_axis(axis))

if not np.issubdtype(arr.dtype, np.number):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use is_integer_dtype

Copy link
Contributor Author

@kykosic kykosic Mar 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause a new exception when an array of float values is passed to .iloc. Currently float values are allowed and the array is cast to integer dtype. This will cause some regression.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then you need to thread this in the appropriate place

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alt you could check is_numeric_dtype

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright I tried to reorganize it a bit, let me know if I misunderstood what you meant.

raise IndexError(".iloc requires integer indexers, got "
"{arr}".format(arr=arr))

if len(arr) and (arr.max() >= len_axis or arr.min() < -len_axis):
raise IndexError("positional indexers are out-of-bounds")
else:
Expand Down
34 changes: 34 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,40 @@ def check(result, expected):
with pytest.raises(IndexError, match=msg):
dfl.iloc[:, 4]

def test_iloc_non_integer(self):

# iloc should throw IndexError if non-numeric list is passed
df = DataFrame(np.random.random_sample((20, 5)), columns=list('ABCDE'))

msg = '.iloc requires integer indexers, got'
with pytest.raises(IndexError, match=msg):
df.iloc[:, ['A', 'D']]
with pytest.raises(IndexError, match=msg):
df.iloc[['1', '2'], :]
with pytest.raises(IndexError, match=msg):
df.iloc[[pd.datetime(2019, 1, 1)], :]

# Numeric or booleans should not raise errors
result = df.iloc[:, [0, 1]]
expected = df.loc[:, ['A', 'B']]
tm.assert_frame_equal(result, expected)

result = df.iloc[[0, 1], [0, 1, 3]]
expected = df.loc[[0, 1], ['A', 'B', 'D']]
tm.assert_frame_equal(result, expected)

result = df.iloc[:, [True, False, False, True]]
expected = df.loc[:, ['A', 'D']]
tm.assert_frame_equal(result, expected)

result = df.iloc[:, [0, 0.5]]
expected = df.loc[:, ['A', 'A']]
tm.assert_frame_equal(result, expected)

result = df.iloc[[True, False, True], :]
expected = df.loc[[0, 2], :]
tm.assert_frame_equal(result, expected)

def test_iloc_getitem_int(self):

# integer
Expand Down