Skip to content

Commit 41b77ca

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 41b77ca

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
@@ -1595,6 +1595,27 @@ 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+
Return Series values by list or array of integers
1601+
1602+
Parameters
1603+
----------
1604+
key_list : list-like positional indexer
1605+
axis : int (can only be zero)
1606+
1607+
Returns
1608+
-------
1609+
Series object
1610+
"""
1611+
1612+
# validate list bounds
1613+
self._is_valid_list_like(key_list, axis)
1614+
1615+
# force an actual list
1616+
key_list = list(key_list)
1617+
return self.obj.take(key_list, axis=axis, convert=False)
1618+
15981619
def _getitem_axis(self, key, axis=0):
15991620

16001621
if isinstance(key, slice):
@@ -1605,27 +1626,20 @@ def _getitem_axis(self, key, axis=0):
16051626
self._has_valid_type(key, axis)
16061627
return self._getbool_axis(key, axis=axis)
16071628

1608-
# 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)
1629+
# a list of integers
1630+
elif is_list_like_indexer(key):
1631+
return self._get_list_axis(key, axis=axis)
16191632

1620-
else:
1621-
key = self._convert_scalar_indexer(key, axis)
1633+
# a single integer
1634+
else:
1635+
key = self._convert_scalar_indexer(key, axis)
16221636

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

1627-
# validate the location
1628-
self._is_valid_integer(key, axis)
1641+
# validate the location
1642+
self._is_valid_integer(key, axis)
16291643

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

0 commit comments

Comments
 (0)