Skip to content

Commit 45add48

Browse files
committed
index_what_you_can
1 parent 125c414 commit 45add48

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

pandas/core/indexes/multi.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -1035,11 +1035,13 @@ def is_lexsorted(self):
10351035
"""
10361036
return self.lexsort_depth == self.nlevels
10371037

1038-
def is_lexsorted_for_tuple(self, tup):
1038+
def _true_slices_indices(self, tup):
10391039
"""
1040-
Return True if we are correctly lexsorted given the passed tuple
1040+
Return indices of (non-trivial) slices in "tup"
10411041
"""
1042-
return len(tup) <= self.lexsort_depth
1042+
slices = lambda k : (isinstance(k, slice)
1043+
and (k.start is not None or k.stop is not None))
1044+
return [(i if slices(k) else -1) for (i, k) in enumerate(tup)]
10431045

10441046
@cache_readonly
10451047
def lexsort_depth(self):
@@ -2262,12 +2264,12 @@ def get_locs(self, tup):
22622264
"""
22632265

22642266
# must be lexsorted to at least as many levels
2265-
if not self.is_lexsorted_for_tuple(tup):
2267+
last_slice = max(self._true_slices_indices(tup))
2268+
if last_slice > self.lexsort_depth:
22662269
raise UnsortedIndexError('MultiIndex Slicing requires the index '
2267-
'to be fully lexsorted tuple len ({0}), '
2270+
'to be lexsorted: slicing on level ({0}), '
22682271
'lexsort depth ({1})'
2269-
.format(len(tup), self.lexsort_depth))
2270-
2272+
.format(last_slice, self.lexsort_depth))
22712273
# indexer
22722274
# this is the list of all values that we want to select
22732275
n = len(self)

0 commit comments

Comments
 (0)