Skip to content

Commit 903a6ea

Browse files
toobazvictor
authored and
victor
committed
REF: No need to delegate to index check of whether an int is an int (pandas-dev#21982)
1 parent 89a73fa commit 903a6ea

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

pandas/core/indexing.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,25 @@ def _getitem_scalar(self, key):
21242124
return values
21252125

21262126
def _validate_integer(self, key, axis):
2127-
# return a boolean if we have a valid integer indexer
2127+
"""
2128+
Check that 'key' is a valid position in the desired axis.
2129+
2130+
Parameters
2131+
----------
2132+
key : int
2133+
Requested position
2134+
axis : int
2135+
Desired axis
2136+
2137+
Returns
2138+
-------
2139+
None
2140+
2141+
Raises
2142+
------
2143+
IndexError
2144+
If 'key' is not a valid position in axis 'axis'
2145+
"""
21282146

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

22162234
# a single integer
22172235
else:
2218-
key = self._convert_scalar_indexer(key, axis)
2219-
22202236
if not is_integer(key):
22212237
raise TypeError("Cannot index by location index with a "
22222238
"non-integer key")

pandas/tests/indexing/test_floats.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_scalar_error(self):
5050
def f():
5151
s.iloc[3.0]
5252
tm.assert_raises_regex(TypeError,
53-
'cannot do positional indexing',
53+
'Cannot index by location index',
5454
f)
5555

5656
def f():

pandas/tests/indexing/test_iloc.py

+12
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ def test_iloc_getitem_neg_int(self):
126126
typs=['labels', 'mixed', 'ts', 'floats', 'empty'],
127127
fails=IndexError)
128128

129+
@pytest.mark.parametrize('dims', [1, 2])
130+
def test_iloc_getitem_invalid_scalar(self, dims):
131+
# GH 21982
132+
133+
if dims == 1:
134+
s = Series(np.arange(10))
135+
else:
136+
s = DataFrame(np.arange(100).reshape(10, 10))
137+
138+
tm.assert_raises_regex(TypeError, 'Cannot index by location index',
139+
lambda: s.iloc['a'])
140+
129141
def test_iloc_array_not_mutating_negative_indices(self):
130142

131143
# GH 21867

0 commit comments

Comments
 (0)