Skip to content

Commit 7afe82c

Browse files
committed
CLN: clean up type checking for MultiIndex indexing
closes #GH15452
1 parent 5667a3a commit 7afe82c

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2209,7 +2209,7 @@ def query(self, expr, inplace=False, **kwargs):
22092209

22102210
try:
22112211
new_data = self.loc[res]
2212-
except ValueError:
2212+
except (ValueError, NotImplementedError):
22132213
# when res is multi-dimensional loc raises, but this is sometimes a
22142214
# valid query
22152215
new_data = self[res]

pandas/core/indexing.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,37 +1550,28 @@ def _getitem_axis(self, key, axis=0):
15501550
elif is_bool_indexer(key):
15511551
return self._getbool_axis(key, axis=axis)
15521552
elif is_list_like_indexer(key):
1553+
if isinstance(key, ABCDataFrame):
1554+
# GH 15438
1555+
raise NotImplementedError("Indexing a with a DataFrame key is "
1556+
"not implemented")
1557+
elif hasattr(key, 'ndim') and key.ndim > 1:
1558+
raise NotImplementedError("Indexing with a multidimensional "
1559+
"key is not implemented")
15531560

15541561
# convert various list-like indexers
15551562
# to a list of keys
15561563
# we will use the *values* of the object
15571564
# and NOT the index if its a PandasObject
15581565
if isinstance(labels, MultiIndex):
1559-
1560-
if isinstance(key, (ABCSeries, np.ndarray)) and key.ndim <= 1:
1561-
# Series, or 0,1 ndim ndarray
1566+
if isinstance(key, (ABCSeries, np.ndarray)) and key.ndim != 1:
1567+
# Series or 1-dim ndarray
15621568
# GH 14730
15631569
key = list(key)
1564-
elif isinstance(key, ABCDataFrame):
1565-
# GH 15438
1566-
raise NotImplementedError("Indexing a MultiIndex with a "
1567-
"DataFrame key is not "
1568-
"implemented")
1569-
elif hasattr(key, 'ndim') and key.ndim > 1:
1570-
raise NotImplementedError("Indexing a MultiIndex with a "
1571-
"multidimensional key is not "
1572-
"implemented")
1573-
1574-
if (not isinstance(key, tuple) and len(key) > 1 and
1575-
not isinstance(key[0], tuple)):
1576-
key = tuple([key])
1570+
if not isinstance(key, tuple):
1571+
return self._getitem_iterable(key, axis=axis)
15771572

15781573
# an iterable multi-selection
1579-
if not (isinstance(key, tuple) and isinstance(labels, MultiIndex)):
1580-
1581-
if hasattr(key, 'ndim') and key.ndim > 1:
1582-
raise ValueError('Cannot index with multidimensional key')
1583-
1574+
else:
15841575
return self._getitem_iterable(key, axis=axis)
15851576

15861577
# nested tuple slicing

0 commit comments

Comments
 (0)