Skip to content

Commit eec4dd5

Browse files
committed
CLN: removing more useless checks
1 parent 06141c0 commit eec4dd5

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

pandas/core/indexing.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def _get_setitem_indexer(self, key):
177177

178178
try:
179179
return self._convert_to_indexer(key, is_setter=True)
180-
except TypeError as e:
180+
except ValueError as e:
181181

182182
# invalid indexer type vs 'other' indexing errors
183183
if 'cannot do' in str(e):
@@ -2090,8 +2090,8 @@ def _validate_key(self, key, axis):
20902090
if len(arr) and (arr.max() >= l or arr.min() < -l):
20912091
raise IndexError("positional indexers are out-of-bounds")
20922092
else:
2093-
raise ValueError("Can only index by location with "
2094-
"a [{types}]".format(types=self._valid_types))
2093+
raise TypeError("Can only index by location with "
2094+
"a [{types}]".format(types=self._valid_types))
20952095

20962096
def _has_valid_setitem_indexer(self, indexer):
20972097
self._has_valid_positional_setitem_indexer(indexer)
@@ -2242,24 +2242,31 @@ def _getitem_axis(self, key, axis=None):
22422242

22432243
return self._get_loc(key, axis=axis)
22442244

2245-
def _convert_to_indexer(self, obj, axis=None, is_setter=False):
2246-
""" much simpler as we only have to deal with our valid types """
2247-
if axis is None:
2248-
axis = self.axis or 0
2245+
def _convert_to_indexer(self, key, axis=None, is_setter=False):
2246+
"""
2247+
iloc needs no conversion of keys: this method is for compatibility
2248+
with other indexer types, and only validates the key.
22492249
2250-
# make need to convert a float key
2251-
if isinstance(obj, slice):
2252-
return self._convert_slice_indexer(obj, axis)
2250+
Parameters
2251+
----------
2252+
key : int, int slice, listlike of integers, or boolean array
2253+
A valid key for iloc
2254+
axis : int
2255+
Dimension on which the indexing is being made
22532256
2254-
elif is_float(obj):
2255-
return self._convert_scalar_indexer(obj, axis)
2257+
Returns
2258+
-------
2259+
'key' itself
22562260
2257-
try:
2258-
self._validate_key(obj, axis)
2259-
return obj
2260-
except ValueError:
2261-
raise ValueError("Can only index by location with "
2262-
"a [{types}]".format(types=self._valid_types))
2261+
Raises
2262+
------
2263+
Same as _iLocIndexer._validate_key()
2264+
"""
2265+
if axis is None:
2266+
axis = self.axis or 0
2267+
2268+
self._validate_key(key, axis)
2269+
return key
22632270

22642271

22652272
class _ScalarAccessIndexer(_NDFrameIndexer):

pandas/tests/indexing/test_iloc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ def test_iloc_getitem_labelled_frame(self):
447447
pytest.raises(IndexError, df.iloc.__getitem__, tuple([10, 5]))
448448

449449
# trying to use a label
450-
pytest.raises(ValueError, df.iloc.__getitem__, tuple(['j', 'D']))
450+
pytest.raises(TypeError, df.iloc.__getitem__, tuple(['j', 'D']))
451451

452452
def test_iloc_getitem_doc_issue(self):
453453

pandas/tests/indexing/test_panel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def f():
6868
pytest.raises(IndexError, f)
6969

7070
# trying to use a label
71-
with pytest.raises(ValueError):
71+
with pytest.raises(TypeError):
7272
p.iloc[tuple(['j', 'D'])]
7373

7474
# GH

0 commit comments

Comments
 (0)