Skip to content

Commit a5a9c5c

Browse files
committed
CLN: refactor _getitem_axis() of _iLocIndexer class
_get_list_axis() is factored out of _getitem_axis() to handle list-like type of input.
1 parent ff52ac7 commit a5a9c5c

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

pandas/core/indexing.py

+18-17
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,15 @@ def _get_slice_axis(self, slice_obj, axis=0):
15951595
else:
15961596
return self.obj.take(slice_obj, axis=axis, convert=False)
15971597

1598+
def _get_list_axis(self, key_list, axis=0):
1599+
1600+
# validate list bounds
1601+
self._is_valid_list_like(key_list, axis)
1602+
1603+
# force an actual list
1604+
key_list = list(key_list)
1605+
return self.obj.take(key_list, axis=axis, convert=False)
1606+
15981607
def _getitem_axis(self, key, axis=0):
15991608

16001609
if isinstance(key, slice):
@@ -1606,26 +1615,18 @@ def _getitem_axis(self, key, axis=0):
16061615
return self._getbool_axis(key, axis=axis)
16071616

16081617
# a single integer or a list of integers
1609-
else:
1610-
1611-
if is_list_like_indexer(key):
1612-
1613-
# validate list bounds
1614-
self._is_valid_list_like(key, axis)
1615-
1616-
# force an actual list
1617-
key = list(key)
1618-
return self.obj.take(key, axis=axis, convert=False)
1618+
elif is_list_like_indexer(key):
1619+
return self._get_list_axis(key, axis=axis)
16191620

1620-
else:
1621-
key = self._convert_scalar_indexer(key, axis)
1621+
else:
1622+
key = self._convert_scalar_indexer(key, axis)
16221623

1623-
if not is_integer(key):
1624-
raise TypeError("Cannot index by location index with a "
1625-
"non-integer key")
1624+
if not is_integer(key):
1625+
raise TypeError("Cannot index by location index with a "
1626+
"non-integer key")
16261627

1627-
# validate the location
1628-
self._is_valid_integer(key, axis)
1628+
# validate the location
1629+
self._is_valid_integer(key, axis)
16291630

16301631
return self._get_loc(key, axis=axis)
16311632

0 commit comments

Comments
 (0)