Skip to content

Commit b083fee

Browse files
committed
PERF: speed up MultiIndex.is_monotonic by 50x
1 parent 5bd57f9 commit b083fee

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

doc/source/whatsnew/v1.0.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ Performance improvements
7070
~~~~~~~~~~~~~~~~~~~~~~~~
7171

7272
- Performance improvement in indexing with a non-unique :class:`IntervalIndex` (:issue:`27489`)
73-
-
73+
- Performance improvement in `MultiIndex.is_monotonic` (:issue:`27495`)
74+
7475

7576
.. _whatsnew_1000.bug_fixes:
7677

pandas/core/indexes/multi.py

+6
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,12 @@ def is_monotonic_increasing(self):
13591359
increasing) values.
13601360
"""
13611361

1362+
if all(x.is_monotonic for x in self.levels):
1363+
# If each level is sorted, we can operate on the codes directly. GH27495
1364+
return libalgos.is_lexsorted(
1365+
[x.astype("int64", copy=False) for x in self.codes]
1366+
)
1367+
13621368
# reversed() because lexsort() wants the most significant key last.
13631369
values = [
13641370
self._get_level_values(i).values for i in reversed(range(len(self.levels)))

0 commit comments

Comments
 (0)