Skip to content

CLN: implement _getitem_tuple_same_dim #31911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 21 additions & 28 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,25 @@ def _handle_lowerdim_multi_index_axis0(self, tup: Tuple):

return None

def _getitem_tuple_same_dim(self, tup: Tuple):
"""
Index with indexers that should return an object of the same dimension
as self.obj.

This is only called after a failed call to _getitem_lowerdim.
"""
retval = self.obj
for i, key in enumerate(tup):
if com.is_null_slice(key):
continue

retval = getattr(retval, self.name)._getitem_axis(key, axis=i)
# We should never have retval.ndim < self.ndim, as that should
# be handled by the _getitem_lowerdim call above.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth adding an assert here?

assert retval.ndim == self.ndim

return retval

def _getitem_lowerdim(self, tup: Tuple):

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

# no shortcut needed
retval = self.obj
for i, key in enumerate(tup):
if com.is_null_slice(key):
continue

retval = getattr(retval, self.name)._getitem_axis(key, axis=i)

return retval
return self._getitem_tuple_same_dim(tup)

def _getitem_axis(self, key, axis: int):
key = item_from_zerodim(key)
Expand Down Expand Up @@ -1468,25 +1479,7 @@ def _getitem_tuple(self, tup: Tuple):
except IndexingError:
pass

retval = self.obj
axis = 0
for i, key in enumerate(tup):
if com.is_null_slice(key):
axis += 1
continue

retval = getattr(retval, self.name)._getitem_axis(key, axis=axis)

# if the dim was reduced, then pass a lower-dim the next time
if retval.ndim < self.ndim:
# TODO: this is never reached in tests; can we confirm that
# it is impossible?
axis -= 1

# try to get for the next axis
axis += 1

return retval
return self._getitem_tuple_same_dim(tup)

def _get_list_axis(self, key, axis: int):
"""
Expand Down