Skip to content

PERF: speed up MultiIndex.is_monotonic by 50x #27495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ Performance improvements
~~~~~~~~~~~~~~~~~~~~~~~~

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


.. _whatsnew_1000.bug_fixes:

Expand Down
6 changes: 6 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,12 @@ def is_monotonic_increasing(self):
increasing) values.
"""

if all(x.is_monotonic for x in self.levels):
# If each level is sorted, we can operate on the codes directly. GH27495
return libalgos.is_lexsorted(
[x.astype("int64", copy=False) for x in self.codes]
)

# reversed() because lexsort() wants the most significant key last.
values = [
self._get_level_values(i).values for i in reversed(range(len(self.levels)))
Expand Down