Skip to content
Merged
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
60 changes: 60 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,36 @@ def _get_level_number(self, level) -> int:
def is_monotonic_increasing(self) -> bool:
"""
Return a boolean if the values are equal or increasing.

For a MultiIndex, this checks whether the tuples are in
lexicographically non-decreasing order: the first level is
compared first, ties are broken by the second level, and so on.

Returns
-------
bool

See Also
--------
MultiIndex.is_monotonic_decreasing : Check if the values are
equal or decreasing.
MultiIndex.sortlevel : Sort a MultiIndex by the requested level.

Examples
--------
>>> mi = pd.MultiIndex.from_arrays([["a", "b", "c"], [1, 2, 3]])
>>> mi.is_monotonic_increasing
True

Ties at one level are broken by the next level:

>>> mi = pd.MultiIndex.from_arrays([[1, 1, 2], [1, 2, 1]])
>>> mi.is_monotonic_increasing
True

>>> mi = pd.MultiIndex.from_arrays([[1, 1, 2], [2, 1, 3]])
>>> mi.is_monotonic_increasing
False
"""
if any(-1 in code for code in self.codes):
return False
Expand Down Expand Up @@ -1846,6 +1876,36 @@ def is_monotonic_increasing(self) -> bool:
def is_monotonic_decreasing(self) -> bool:
"""
Return a boolean if the values are equal or decreasing.

For a MultiIndex, this checks whether the tuples are in
lexicographically non-increasing order: the first level is
compared first, ties are broken by the second level, and so on.

Returns
-------
bool

See Also
--------
MultiIndex.is_monotonic_increasing : Check if the values are
equal or increasing.
MultiIndex.sortlevel : Sort a MultiIndex by the requested level.

Examples
--------
>>> mi = pd.MultiIndex.from_arrays([["c", "b", "a"], [3, 2, 1]])
>>> mi.is_monotonic_decreasing
True

Ties at one level are broken by the next level:

>>> mi = pd.MultiIndex.from_arrays([[2, 1, 1], [1, 2, 1]])
>>> mi.is_monotonic_decreasing
True

>>> mi = pd.MultiIndex.from_arrays([[2, 1, 1], [3, 1, 2]])
>>> mi.is_monotonic_decreasing
False
"""
# monotonic decreasing if and only if reverse is monotonic increasing
return self[::-1].is_monotonic_increasing
Expand Down
Loading