Skip to content

Commit 9a69ca5

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 5b5ead4 commit 9a69ca5

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

pandas/core/indexing.py

+32-18
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,27 @@ def _get_slice_axis(self, slice_obj, axis=0):
15961596
else:
15971597
return self.obj.take(slice_obj, axis=axis, convert=False)
15981598

1599+
def _get_list_axis(self, key_list, axis=0):
1600+
"""
1601+
Return Series values by list or array of integers
1602+
1603+
Parameters
1604+
----------
1605+
key_list : list-like positional indexer
1606+
axis : int (can only be zero)
1607+
1608+
Returns
1609+
-------
1610+
Series object
1611+
"""
1612+
1613+
# validate list bounds
1614+
self._is_valid_list_like(key_list, axis)
1615+
1616+
# force an actual list
1617+
key_list = list(key_list)
1618+
return self.obj.take(key_list, axis=axis, convert=False)
1619+
15991620
def _getitem_axis(self, key, axis=0):
16001621

16011622
if isinstance(key, slice):
@@ -1606,27 +1627,20 @@ def _getitem_axis(self, key, axis=0):
16061627
self._has_valid_type(key, axis)
16071628
return self._getbool_axis(key, axis=axis)
16081629

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

1621-
else:
1622-
key = self._convert_scalar_indexer(key, axis)
1634+
# a single integer
1635+
else:
1636+
key = self._convert_scalar_indexer(key, axis)
16231637

1624-
if not is_integer(key):
1625-
raise TypeError("Cannot index by location index with a "
1626-
"non-integer key")
1638+
if not is_integer(key):
1639+
raise TypeError("Cannot index by location index with a "
1640+
"non-integer key")
16271641

1628-
# validate the location
1629-
self._is_valid_integer(key, axis)
1642+
# validate the location
1643+
self._is_valid_integer(key, axis)
16301644

16311645
return self._get_loc(key, axis=axis)
16321646

0 commit comments

Comments
 (0)