Skip to content

Commit 95d1c6b

Browse files
committed
CLN: clean up type checking for MultiIndex indexing
closes #GH15452
1 parent 4ca9fcd commit 95d1c6b

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
@@ -2214,7 +2214,7 @@ def query(self, expr, inplace=False, **kwargs):
22142214

22152215
try:
22162216
new_data = self.loc[res]
2217-
except ValueError:
2217+
except (ValueError, NotImplementedError):
22182218
# when res is multi-dimensional loc raises, but this is sometimes a
22192219
# valid query
22202220
new_data = self[res]

pandas/core/indexing.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,37 +1511,28 @@ def _getitem_axis(self, key, axis=0):
15111511
elif is_bool_indexer(key):
15121512
return self._getbool_axis(key, axis=axis)
15131513
elif is_list_like_indexer(key):
1514+
if isinstance(key, ABCDataFrame):
1515+
# GH 15438
1516+
raise NotImplementedError("Indexing a with a DataFrame key is "
1517+
"not implemented")
1518+
elif hasattr(key, 'ndim') and key.ndim > 1:
1519+
raise NotImplementedError("Indexing with a multidimensional "
1520+
"key is not implemented")
15141521

15151522
# convert various list-like indexers
15161523
# to a list of keys
15171524
# we will use the *values* of the object
15181525
# and NOT the index if its a PandasObject
15191526
if isinstance(labels, MultiIndex):
1520-
1521-
if isinstance(key, (ABCSeries, np.ndarray)) and key.ndim <= 1:
1522-
# Series, or 0,1 ndim ndarray
1527+
if isinstance(key, (ABCSeries, np.ndarray)) and key.ndim != 1:
1528+
# Series or 1-dim ndarray
15231529
# GH 14730
15241530
key = list(key)
1525-
elif isinstance(key, ABCDataFrame):
1526-
# GH 15438
1527-
raise NotImplementedError("Indexing a MultiIndex with a "
1528-
"DataFrame key is not "
1529-
"implemented")
1530-
elif hasattr(key, 'ndim') and key.ndim > 1:
1531-
raise NotImplementedError("Indexing a MultiIndex with a "
1532-
"multidimensional key is not "
1533-
"implemented")
1534-
1535-
if (not isinstance(key, tuple) and len(key) > 1 and
1536-
not isinstance(key[0], tuple)):
1537-
key = tuple([key])
1531+
if not isinstance(key, tuple):
1532+
return self._getitem_iterable(key, axis=axis)
15381533

15391534
# an iterable multi-selection
1540-
if not (isinstance(key, tuple) and isinstance(labels, MultiIndex)):
1541-
1542-
if hasattr(key, 'ndim') and key.ndim > 1:
1543-
raise ValueError('Cannot index with multidimensional key')
1544-
1535+
else:
15451536
return self._getitem_iterable(key, axis=axis)
15461537

15471538
# nested tuple slicing

0 commit comments

Comments
 (0)