Skip to content

REF: No need to delegate to index check of whether an int is an int #21982

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 2 commits into from
Jul 20, 2018
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
22 changes: 19 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2124,7 +2124,25 @@ def _getitem_scalar(self, key):
return values

def _validate_integer(self, key, axis):
# return a boolean if we have a valid integer indexer
"""
Check that 'key' is a valid position in the desired axis.

Parameters
----------
key : int
Requested position
axis : int
Desired axis

Returns
-------
None

Raises
------
IndexError
If 'key' is not a valid position in axis 'axis'
"""

ax = self.obj._get_axis(axis)
l = len(ax)
Expand Down Expand Up @@ -2215,8 +2233,6 @@ def _getitem_axis(self, key, axis=None):

# a single integer
else:
key = self._convert_scalar_indexer(key, axis)
Copy link
Member

Choose a reason for hiding this comment

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

Can you check if there is any remaining occasion where _convert_scalar_indexer is used for iloc (in principle it shouldn't?). Because if not, _convert_scalar_indexer has some redundant code

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, there is, and it was related to a commit I pushed but then removed, because unfortunately it broke some tests and I don't have time to check them in the next days. So I went for the obvious change only, will add the rest in some other PR (which I think will be quite large, as I will also touch code in indexes).

Copy link
Member

Choose a reason for hiding this comment

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

OK. Can you point to where it is currently used? (to have this on record)
Is it worth to add a todo comment there?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll rebase my other PR on this once merged, push it and open an issue referring to it


if not is_integer(key):
raise TypeError("Cannot index by location index with a "
"non-integer key")
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/test_floats.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_scalar_error(self):
def f():
s.iloc[3.0]
tm.assert_raises_regex(TypeError,
'cannot do positional indexing',
'Cannot index by location index',
Copy link
Contributor

Choose a reason for hiding this comment

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

side issue, we have 2 different versions of this error message (the original also happens), so these should be the same (future PR). I don't think we actually need to distinguish them.

Copy link
Member Author

Choose a reason for hiding this comment

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

We already have both messages in the code (I did not write a new message).

The only difference is that now this case raises the correct one.

f)

def f():
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ def test_iloc_getitem_neg_int(self):
typs=['labels', 'mixed', 'ts', 'floats', 'empty'],
fails=IndexError)

@pytest.mark.parametrize('dims', [1, 2])
def test_iloc_getitem_invalid_scalar(self, dims):
# GH 21982

if dims == 1:
s = Series(np.arange(10))
else:
s = DataFrame(np.arange(100).reshape(10, 10))

tm.assert_raises_regex(TypeError, 'Cannot index by location index',
lambda: s.iloc['a'])

def test_iloc_array_not_mutating_negative_indices(self):

# GH 21867
Expand Down