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

BUG/PERF: use lexsort_indexer in MultiIndex.argsort #48495

Merged
merged 20 commits into from
Oct 7, 2022

Conversation

lukemanley
Copy link
Member

cc: @phofl, @mroeschke - follow on to #48406

Had I been aware of lexsort_indexer I would have used that in #48406. Using lexsort_indexer handles pd.NA (np.lexsort raises) and has better perf for nullable types (Int64 on par with int64).

While the perf for non-nullable types (e.g. int64) suffers a bit, this is still much faster than prior to #48406.

       before           after         ratio
     [4e72340d]       [ead3c00d]
     <main>           <multiindex-argsort>
+     1.78±0.03ms       3.59±0.4ms     2.01  multiindex_object.SortValues.time_sort_values('int64')
-      26.7±0.5ms       3.51±0.5ms     0.13  multiindex_object.SortValues.time_sort_values('Int64')

Copy link
Member

@phofl phofl left a comment

Choose a reason for hiding this comment

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

Special casing only numpy dtypes makes it ugly, correct?

@lukemanley
Copy link
Member Author

Special casing only numpy dtypes makes it ugly, correct?

That was my thinking. DataFrame.sort_index uses the same approach with lexsort_indexer and does not special case numpy dtypes.

@phofl
Copy link
Member

phofl commented Sep 10, 2022

Ok, sounds reasonable

@phofl
Copy link
Member

phofl commented Sep 10, 2022

Can you merge main to get ci green?

@lukemanley lukemanley added Performance Memory or execution speed performance MultiIndex NA - MaskedArrays Related to pd.NA and nullable extension arrays Bug labels Sep 10, 2022
Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

Would be good to have a test that calls MultiIndex.argsort directly

@lukemanley
Copy link
Member Author

Would be good to have a test that calls MultiIndex.argsort directly

added an explicit argsort test

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

LGTM. Merge when ready @phofl

@mroeschke mroeschke added this to the 1.6 milestone Sep 13, 2022
@phofl
Copy link
Member

phofl commented Sep 13, 2022

Just merged the other pr. Could you maybe run asvs once more? Union has a bunch of code paths, want to see if this is affected

@lukemanley
Copy link
Member Author

Just merged the other pr. Could you maybe run asvs once more? Union has a bunch of code paths, want to see if this is affected

I don’t think this touches union. Were you thinking of #48504 ?

@lukemanley
Copy link
Member Author

@phofl, @mroeschke

This is now failing following the merge of #48505. The test failing is test_union_sort_other_incomparable which expects to see a RunTimeWarning from MultiIndex._union. However, this uncovers some inconsistency in the main branch.

Here is an example:

import pandas as pd
idx = pd.MultiIndex.from_product([[pd.Timestamp("2000"), 1], ["a", "b"]])
df = pd.DataFrame({"A": 1}, index=idx)

# on main, this returns without warning/error as it goes through lexsort_indexer
df.sort_index()

# on main, this raises a TypeError as it goes through np.lexsort
df.index.sort_values()

With this PR the behavior will be consistent as we'll be sending both through lexsort_indexer. I've removed the expectation for a RunTimeWarning from the test. Let me know if that doesn't make sense.

doc/source/whatsnew/v1.6.0.rst Show resolved Hide resolved
pandas/core/indexes/multi.py Outdated Show resolved Hide resolved
@@ -280,3 +281,24 @@ def test_remove_unused_levels_with_nan():
result = idx.levels
expected = FrozenList([["a", np.nan], [4]])
assert str(result) == str(expected)


def test_sort_values_nan():
Copy link
Member

Choose a reason for hiding this comment

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

Can you add gh refs

Copy link
Member Author

Choose a reason for hiding this comment

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

Added refs

Copy link
Member

Choose a reason for hiding this comment

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

Other tests as well please

@lukemanley
Copy link
Member Author

@phofl - gentle ping. I think this is ready.

@phofl phofl merged commit c0e6baf into pandas-dev:main Oct 7, 2022
@phofl
Copy link
Member

phofl commented Oct 7, 2022

thx. @lukemanley

@mroeschke mroeschke modified the milestones: 1.6, 2.0 Oct 13, 2022
@lukemanley lukemanley deleted the multiindex-argsort-nullable branch October 26, 2022 10:18
noatamir pushed a commit to noatamir/pandas that referenced this pull request Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug MultiIndex NA - MaskedArrays Related to pd.NA and nullable extension arrays Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants