Skip to content

Fix regression for is_monotonic_increasing with nan in MultiIndex #37221

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 7 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
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 @@ -20,6 +20,7 @@ Fixed regressions
- Fixed regression in :class:`RollingGroupby` with ``sort=False`` not being respected (:issue:`36889`)
- Fixed regression in :meth:`Series.astype` converting ``None`` to ``"nan"`` when casting to string (:issue:`36904`)
- Fixed regression in :class:`RollingGroupby` causing a segmentation fault with Index of dtype object (:issue:`36727`)
- 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 @@ -1539,7 +1539,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 all(
level.is_monotonic and -1 not in code
for (level, code) in zip(self.levels, self.codes)
):
# 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
6 changes: 6 additions & 0 deletions pandas/tests/indexes/multi/test_monotonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,9 @@ def test_is_strictly_monotonic_decreasing():
)
assert idx.is_monotonic_decreasing is True
assert idx._is_strictly_monotonic_decreasing is False


def test_is_monotonic_with_nans():
# GH: 37220
idx = pd.MultiIndex.from_tuples([(np.nan,), (1,)], names=["test"])
assert idx.is_monotonic_increasing is False
Copy link
Member

Choose a reason for hiding this comment

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

Maybe parametrize over different positions for NaN? I think it's probably correct to always return False here, although this is interesting:

[ins] In [32]: idx
Out[32]: Float64Index([nan, 1.0, 2.0], dtype='float64')

[ins] In [33]: idx.sort_values(ascending=True).is_monotonic_increasing
Out[33]: False

Copy link
Member Author

Choose a reason for hiding this comment

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

That really is interesting. With 0.25.3
pd.MultiIndex.from_tuples([(1,), (np.nan,)], names=["test"]).is_monotonic_increasing returned True. We could change that, but that would be more than a Regression fix then?

Copy link
Member

Choose a reason for hiding this comment

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

I think probably fine to call it a regression fix. I would say the previous behavior is not correct in this case (if monotone increasing means x <= y for every adjacent pair of elements, then no sequence with NaN values is monotone increasing, or decreasing for that matter), so we'd only want to fix the case with NaN at the beginning rather than restoring the behavior completely.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks very much for the explanation. Adjusted the code accordingly.