Skip to content
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

Fix regression for is_monotonic_increasing with nan in MultiIndex #37221

Merged
merged 7 commits into from
Oct 27, 2020

Conversation

phofl
Copy link
Member

@phofl phofl commented Oct 18, 2020

Found this while debugging a different issue.

@phofl phofl added MultiIndex Regression Functionality that used to work in a prior pandas version labels Oct 18, 2020
Comment on lines 179 to 182
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.

Copy link
Member

@dsaxton dsaxton left a comment

Choose a reason for hiding this comment

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

Thanks @phofl

@phofl phofl mentioned this pull request Oct 26, 2020
@simonjayhawkins simonjayhawkins added this to the 1.1.4 milestone Oct 26, 2020
@simonjayhawkins
Copy link
Member

@phofl can you resolve conflict.

� Conflicts:
�	doc/source/whatsnew/v1.1.4.rst
@phofl
Copy link
Member Author

phofl commented Oct 26, 2020

@simonjayhawkins Done

@jreback
Copy link
Contributor

jreback commented Oct 26, 2020

this looks fine. can you run some asv's to see if any perf diffs here? (prob don't need to add, want to see the existing ones).

@phofl
Copy link
Member Author

phofl commented Oct 26, 2020

@jreback

 before           after         ratio
     [57c06d9b]       [31ef0b98]
     <36915^2>        <37220>   
-        63.5±5ms       56.5±0.4ms     0.89  multiindex_object.SetOperations.time_operation('non_monotonic', 'string', 'symmetric_difference')

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.
PERFORMANCE INCREASED.

Non monotonic indices increased (not surprising). No changes otherwise

@jreback jreback merged commit d6f646b into pandas-dev:master Oct 27, 2020
@jreback
Copy link
Contributor

jreback commented Oct 27, 2020

thanks @phofl

@simonjayhawkins
Copy link
Member

@meeseeksdev backport 1.1.x

meeseeksmachine pushed a commit to meeseeksmachine/pandas that referenced this pull request Oct 27, 2020
@phofl phofl deleted the 37220 branch October 27, 2020 13:12
simonjayhawkins pushed a commit that referenced this pull request Oct 27, 2020
…an in MultiIndex (#37446)

Co-authored-by: patrick <61934744+phofl@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
MultiIndex Regression Functionality that used to work in a prior pandas version
Projects
None yet
4 participants