Skip to content

Commit 48cb5a9

Browse files
authored
CLN: implement _getitem_tuple_same_dim (#31911)
1 parent ee8b856 commit 48cb5a9

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

pandas/core/indexing.py

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,25 @@ def _handle_lowerdim_multi_index_axis0(self, tup: Tuple):
718718

719719
return None
720720

721+
def _getitem_tuple_same_dim(self, tup: Tuple):
722+
"""
723+
Index with indexers that should return an object of the same dimension
724+
as self.obj.
725+
726+
This is only called after a failed call to _getitem_lowerdim.
727+
"""
728+
retval = self.obj
729+
for i, key in enumerate(tup):
730+
if com.is_null_slice(key):
731+
continue
732+
733+
retval = getattr(retval, self.name)._getitem_axis(key, axis=i)
734+
# We should never have retval.ndim < self.ndim, as that should
735+
# be handled by the _getitem_lowerdim call above.
736+
assert retval.ndim == self.ndim
737+
738+
return retval
739+
721740
def _getitem_lowerdim(self, tup: Tuple):
722741

723742
# we can directly get the axis result since the axis is specified
@@ -1049,15 +1068,7 @@ def _getitem_tuple(self, tup: Tuple):
10491068
if self._multi_take_opportunity(tup):
10501069
return self._multi_take(tup)
10511070

1052-
# no shortcut needed
1053-
retval = self.obj
1054-
for i, key in enumerate(tup):
1055-
if com.is_null_slice(key):
1056-
continue
1057-
1058-
retval = getattr(retval, self.name)._getitem_axis(key, axis=i)
1059-
1060-
return retval
1071+
return self._getitem_tuple_same_dim(tup)
10611072

10621073
def _getitem_axis(self, key, axis: int):
10631074
key = item_from_zerodim(key)
@@ -1468,25 +1479,7 @@ def _getitem_tuple(self, tup: Tuple):
14681479
except IndexingError:
14691480
pass
14701481

1471-
retval = self.obj
1472-
axis = 0
1473-
for i, key in enumerate(tup):
1474-
if com.is_null_slice(key):
1475-
axis += 1
1476-
continue
1477-
1478-
retval = getattr(retval, self.name)._getitem_axis(key, axis=axis)
1479-
1480-
# if the dim was reduced, then pass a lower-dim the next time
1481-
if retval.ndim < self.ndim:
1482-
# TODO: this is never reached in tests; can we confirm that
1483-
# it is impossible?
1484-
axis -= 1
1485-
1486-
# try to get for the next axis
1487-
axis += 1
1488-
1489-
return retval
1482+
return self._getitem_tuple_same_dim(tup)
14901483

14911484
def _get_list_axis(self, key, axis: int):
14921485
"""

0 commit comments

Comments
 (0)