Skip to content

Commit

Permalink
Backport PR #37221: Fix regression for is_monotonic_increasing with n…
Browse files Browse the repository at this point in the history
…an in MultiIndex (#37446)

Co-authored-by: patrick <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl authored Oct 27, 2020
1 parent f87111f commit cded237
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Fixed regressions
- Fixed regression where slicing :class:`DatetimeIndex` raised :exc:`AssertionError` on irregular time series with ``pd.NaT`` or on unsorted indices (:issue:`36953` and :issue:`35509`)
- Fixed regression in certain offsets (:meth:`pd.offsets.Day() <pandas.tseries.offsets.Day>` and below) no longer being hashable (:issue:`37267`)
- Fixed regression in :class:`StataReader` which required ``chunksize`` to be manually set when using an iterator to read a dataset (:issue:`37280`)
- Fixed regression in :attr:`MultiIndex.is_monotonic_increasing` returning wrong results with ``NaN`` in at least one of the levels (:issue:`37220`)

.. ---------------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,10 @@ def is_monotonic_increasing(self) -> bool:
return if the index is monotonic increasing (only equal or
increasing) values.
"""
if all(x.is_monotonic for x in self.levels):
if any(-1 in code for code in self.codes):
return False

if all(level.is_monotonic for level 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]
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexes/multi/test_monotonic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pytest

import pandas as pd
from pandas import Index, MultiIndex
Expand Down Expand Up @@ -174,3 +175,14 @@ def test_is_strictly_monotonic_decreasing():
)
assert idx.is_monotonic_decreasing is True
assert idx._is_strictly_monotonic_decreasing is False


@pytest.mark.parametrize("attr", ["is_monotonic_increasing", "is_monotonic_decreasing"])
@pytest.mark.parametrize(
"values",
[[(np.nan,), (1,), (2,)], [(1,), (np.nan,), (2,)], [(1,), (2,), (np.nan,)]],
)
def test_is_monotonic_with_nans(values, attr):
# GH: 37220
idx = pd.MultiIndex.from_tuples(values, names=["test"])
assert getattr(idx, attr) is False

0 comments on commit cded237

Please sign in to comment.