Skip to content

Commit d4d1e32

Browse files
authored
REF: move _convert_to_indexer to Loc (#31554)
1 parent 07eda6a commit d4d1e32

File tree

1 file changed

+39
-43
lines changed

1 file changed

+39
-43
lines changed

pandas/core/indexing.py

+39-43
Original file line numberDiff line numberDiff line change
@@ -1577,6 +1577,43 @@ def _validate_read_indexer(
15771577
"https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike" # noqa:E501
15781578
)
15791579

1580+
def _convert_to_indexer(self, key, axis: int):
1581+
raise AbstractMethodError(self)
1582+
1583+
1584+
class _LocationIndexer(_NDFrameIndexer):
1585+
_takeable: bool = False
1586+
1587+
def __getitem__(self, key):
1588+
if type(key) is tuple:
1589+
key = tuple(com.apply_if_callable(x, self.obj) for x in key)
1590+
if self._is_scalar_access(key):
1591+
try:
1592+
return self.obj._get_value(*key, takeable=self._takeable)
1593+
except (KeyError, IndexError, AttributeError):
1594+
# AttributeError for IntervalTree get_value
1595+
pass
1596+
return self._getitem_tuple(key)
1597+
else:
1598+
# we by definition only have the 0th axis
1599+
axis = self.axis or 0
1600+
1601+
maybe_callable = com.apply_if_callable(key, self.obj)
1602+
return self._getitem_axis(maybe_callable, axis=axis)
1603+
1604+
def _is_scalar_access(self, key: Tuple):
1605+
raise NotImplementedError()
1606+
1607+
def _getitem_axis(self, key, axis: int):
1608+
raise NotImplementedError()
1609+
1610+
def _getbool_axis(self, key, axis: int):
1611+
# caller is responsible for ensuring non-None axis
1612+
labels = self.obj._get_axis(axis)
1613+
key = check_bool_indexer(labels, key)
1614+
inds = key.nonzero()[0]
1615+
return self.obj._take_with_is_copy(inds, axis=axis)
1616+
15801617
def _convert_to_indexer(self, key, axis: int):
15811618
"""
15821619
Convert indexing key into something we can use to do actual fancy
@@ -1631,15 +1668,8 @@ def _convert_to_indexer(self, key, axis: int):
16311668
# if we are setting and its not a valid location
16321669
# its an insert which fails by definition
16331670

1634-
if self.name == "loc":
1635-
# always valid
1636-
return {"key": key}
1637-
1638-
if key >= self.obj.shape[axis] and not isinstance(labels, ABCMultiIndex):
1639-
# a positional
1640-
raise ValueError("cannot set by positional indexing with enlargement")
1641-
1642-
return key
1671+
# always valid
1672+
return {"key": key}
16431673

16441674
if is_nested_tuple(key, labels):
16451675
return labels.get_locs(key)
@@ -1663,40 +1693,6 @@ def _convert_to_indexer(self, key, axis: int):
16631693
raise
16641694

16651695

1666-
class _LocationIndexer(_NDFrameIndexer):
1667-
_takeable: bool = False
1668-
1669-
def __getitem__(self, key):
1670-
if type(key) is tuple:
1671-
key = tuple(com.apply_if_callable(x, self.obj) for x in key)
1672-
if self._is_scalar_access(key):
1673-
try:
1674-
return self.obj._get_value(*key, takeable=self._takeable)
1675-
except (KeyError, IndexError, AttributeError):
1676-
# AttributeError for IntervalTree get_value
1677-
pass
1678-
return self._getitem_tuple(key)
1679-
else:
1680-
# we by definition only have the 0th axis
1681-
axis = self.axis or 0
1682-
1683-
maybe_callable = com.apply_if_callable(key, self.obj)
1684-
return self._getitem_axis(maybe_callable, axis=axis)
1685-
1686-
def _is_scalar_access(self, key: Tuple):
1687-
raise NotImplementedError()
1688-
1689-
def _getitem_axis(self, key, axis: int):
1690-
raise NotImplementedError()
1691-
1692-
def _getbool_axis(self, key, axis: int):
1693-
# caller is responsible for ensuring non-None axis
1694-
labels = self.obj._get_axis(axis)
1695-
key = check_bool_indexer(labels, key)
1696-
inds = key.nonzero()[0]
1697-
return self.obj._take_with_is_copy(inds, axis=axis)
1698-
1699-
17001696
@Appender(IndexingMixin.loc.__doc__)
17011697
class _LocIndexer(_LocationIndexer):
17021698
_valid_types = (

0 commit comments

Comments
 (0)