DEPR MultiIndex.is_lexsorted and MultiIndex.lexsort_depth by MarcoGorelli · Pull Request #38701 · pandas-dev/pandas (original) (raw)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your review - the difficulty I'm running into is that in pandas/core/indexes/multi.py there's

    if self.sortorder is not None:
        if self.sortorder > self._lexsort_depth():
            raise ValueError(
                "Value for sortorder must be inferior or equal to actual "
                f"lexsort_depth: sortorder {self.sortorder} "
                f"with lexsort_depth {self._lexsort_depth()}"
            )

so I can't just move self.sortorder into self._lexsort_depth else this will never raise, as self._lexsort_depth() would always just return self.sortorder in this snippet.

For now I've factored out the part which is used in the above check into

def _codes_lexsort_depth(self) -> int:
    int64_codes = [ensure_int64(level_codes) for level_codes in self.codes]
    for k in range(self.nlevels, 0, -1):
        if libalgos.is_lexsorted(int64_codes[:k]):
            return k
    return 0

but I'd imagine this will be considered equally confusing. Any suggestions?