8
8
from pandas .core .common import (_format , adjoin as _adjoin , _stringify ,
9
9
_ensure_index , _is_bool_indexer ,
10
10
_asarray_tuplesafe )
11
- from pandas .util .decorators import deprecate
11
+ from pandas .util .decorators import deprecate , cache_readonly
12
12
import pandas .core .common as common
13
13
import pandas ._tseries as _tseries
14
14
@@ -555,8 +555,25 @@ def format(self, space=2, sparsify=True, vertical=False, adjoin=True):
555
555
def is_all_dates (self ):
556
556
return False
557
557
558
- def is_sorted (self ):
559
- raise NotImplementedError
558
+ def is_lexsorted (self ):
559
+ """
560
+ Return True if the labels are lexicographically sorted
561
+ """
562
+ return self .lexsort_depth == self .nlevels
563
+
564
+ @cache_readonly
565
+ def lexsort_depth (self ):
566
+ if self .sortorder is not None :
567
+ if self .sortorder == 0 :
568
+ return self .nlevels
569
+ else :
570
+ return 0
571
+
572
+ for k in range (self .nlevels , 0 , - 1 ):
573
+ if _tseries .is_lexsorted (self .labels [:k ]):
574
+ return k
575
+
576
+ return 0
560
577
561
578
@classmethod
562
579
def from_arrays (cls , arrays , sortorder = None ):
@@ -864,9 +881,6 @@ def slice_locs(self, start=None, end=None):
864
881
-----
865
882
This function assumes that the data is sorted by the first level
866
883
"""
867
- # relax for now
868
- # assert(self.sortorder == 0)
869
-
870
884
if start is None :
871
885
start_slice = 0
872
886
else :
@@ -884,8 +898,9 @@ def slice_locs(self, start=None, end=None):
884
898
return start_slice , end_slice
885
899
886
900
def _partial_tup_index (self , tup , side = 'left' ):
887
- # relax for now
888
- # assert(self.sortorder == 0)
901
+ if len (tup ) > self .lexsort_depth :
902
+ raise Exception ('MultiIndex lexsort depth %d, key was %d long' %
903
+ (self .lexsort_depth , len (tup )))
889
904
890
905
n = len (tup )
891
906
start , end = 0 , len (self )
0 commit comments