From 374a22c1748a3aca7343386dfeadc215c496fd30 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 11 Feb 2020 19:41:44 -0800 Subject: [PATCH 1/2] CLN: implement _getitem_tuple_same_dim --- pandas/core/indexing.py | 48 +++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 44b3c318366d2..9df84e796dc75 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -718,6 +718,24 @@ 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. + + return retval + def _getitem_lowerdim(self, tup: Tuple): # we can directly get the axis result since the axis is specified @@ -1056,15 +1074,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) @@ -1475,25 +1485,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): """ From c1ddf0e01f61e093f5c011446c2eb32c612eff8d Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 12 Feb 2020 08:09:12 -0800 Subject: [PATCH 2/2] assertion --- pandas/core/indexing.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index ca8fa620515a3..b3777e949a08c 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -733,6 +733,7 @@ def _getitem_tuple_same_dim(self, tup: Tuple): 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. + assert retval.ndim == self.ndim return retval