Skip to content

ENH: Add masked engine #49420

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 35 commits into from
Jan 26, 2023
Merged

ENH: Add masked engine #49420

merged 35 commits into from
Jan 26, 2023

Conversation

phofl
Copy link
Member

@phofl phofl commented Oct 31, 2022

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.
       before           after         ratio
     [626b6510]       [efde9dd0]
-        270±10μs          148±3μs     0.55  indexing.NumericMaskedIndexing.time_get_indexer('UInt64', True)
-         210±5μs          106±1μs     0.51  indexing.NumericMaskedIndexing.time_get_indexer('Int64', True)
-        306±50μs          150±3μs     0.49  indexing.NumericMaskedIndexing.time_get_indexer('UInt64', False)
-       340±100μs          158±3μs     0.46  indexing.NumericMaskedIndexing.time_get_indexer_dups('Float64', False)
-        237±20μs        107±0.6μs     0.45  indexing.NumericMaskedIndexing.time_get_indexer('Int64', False)
-        360±70μs          157±2μs     0.44  indexing.NumericMaskedIndexing.time_get_indexer_dups('Float64', True)
-       369±100μs          155±3μs     0.42  indexing.NumericMaskedIndexing.time_get_indexer('Float64', False)
-        330±80μs          137±1μs     0.42  indexing.NumericMaskedIndexing.time_get_indexer_dups('UInt64', True)
-        335±50μs          140±2μs     0.42  indexing.NumericMaskedIndexing.time_get_indexer_dups('UInt64', False)
-       373±200μs          155±3μs     0.42  indexing.NumericMaskedIndexing.time_get_indexer('Float64', True)
-        270±30μs          106±1μs     0.39  indexing.NumericMaskedIndexing.time_get_indexer_dups('Int64', False)
-        310±60μs          110±1μs     0.36  indexing.NumericMaskedIndexing.time_get_indexer_dups('Int64', True)

Lets see if ci passes

@phofl phofl added Enhancement Indexing Related to indexing on series/frames, not to indexes themselves labels Oct 31, 2022
@@ -9,6 +9,11 @@

from pandas._libs import index as libindex

from pandas import (
NA,
Series,
Copy link
Member

Choose a reason for hiding this comment

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

This file shouldn’t depend on series (see docstring)

Copy link
Member

Choose a reason for hiding this comment

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

We still need to construct the data that is passed to the Engine constructor in some way. For the other engines that is a plain numpy array (so not depending on anything outside of pd._libs), but for those new engines for masked arrays that are the data and mask numpy arrays. While it is possible to construct those manually, it is easier to do that through pd.Series(..) (or pd.array(..) would be sufficient), I think? (and this is only for the setup, not for the actual benchmark)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thx missed that docstring

We don't need the actual array since we are passing the underlying NumPy arrays anyway, we can just construct the mask and the data separately

@@ -158,6 +168,8 @@ cdef class IndexEngine:
return self._get_loc_duplicates(val)

try:
if self.mask is not None and val is C_NA:
return self.mapping.get_na()
Copy link
Member

Choose a reason for hiding this comment

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

can this go outside the try/except?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, thx

):
return libindex.ExtensionEngine(target_values)
if isinstance(target_values, ExtensionArray):
is_masked = hasattr(target_values, "_mask")
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 check directly for e.g. BaseMaskedArray here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, have to import BaseMaskedArray locally though

):
return libindex.ExtensionEngine(target_values)
if isinstance(target_values, ExtensionArray):
from pandas.core.arrays import BaseMaskedArray
Copy link
Member

Choose a reason for hiding this comment

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

cant do this up top?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, circular import...

Copy link
Member

Choose a reason for hiding this comment

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

hmm that sucks. not for this PR but i bet either Categorical or IntervalArray are importing something from indexes and it would be reasonable to make the runtime imports happen there

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep sounds sensible

if isinstance(vals, StringArray):
# GH#45652 much more performant than ExtensionEngine
return vals._ndarray
if type(self) is Index and isinstance(self._values, ExtensionArray):
return self._values.astype(object)
Copy link
Member

Choose a reason for hiding this comment

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

IIRC _can_use_libjoin will make it so we don't go down this path

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but we can not use this in one case, because the no use lib join path does not work there for non interval, so we have to go through object and end up here

Copy link
Member Author

Choose a reason for hiding this comment

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

line 4444

@phofl
Copy link
Member Author

phofl commented Dec 19, 2022

This is less ugly now :)

@@ -4762,8 +4779,9 @@ def _get_engine_target(self) -> ArrayLike:
# GH#45652 much more performant than ExtensionEngine
return vals._ndarray
if type(self) is Index and isinstance(self._values, ExtensionArray):
# TODO(ExtensionIndex): remove special-case, just use self._values
return self._values.astype(object)
if not isinstance(self._values, BaseMaskedArray):
Copy link
Member

Choose a reason for hiding this comment

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

an this be combined with the previous condition?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, found this a bit easier to read, but no strong opinion

@phofl
Copy link
Member Author

phofl commented Jan 16, 2023

cc @jbrockmendel Could you have another look?

indices = {
True: Index(range(N), dtype=dtype),
False: Index(
list(range(50)) + [54, 53, 52, 51] + list(range(55, N - 1)), dtype=dtype
Copy link
Member

Choose a reason for hiding this comment

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

NBD but i think if you do this at the class level outside of setup it might only construct once. might add up

Copy link
Member Author

Choose a reason for hiding this comment

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

Do we run into risks of caching stuff on the Index level that could impact performance?

Copy link
Member

Choose a reason for hiding this comment

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

its possible. could just create the arrays instead of the Index objects

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, moved the list construction outside. We should probably think about going over the asvs, we are doing this in a lot of places.

@@ -9,6 +9,8 @@

from pandas._libs import index as libindex

from pandas.core.arrays import BaseMaskedArray
Copy link
Member

Choose a reason for hiding this comment

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

this renders the file-level docstring not-quite accurate. It shouldn't affect the main point of the docstring, but might be worth noting.

Copy link
Member Author

Choose a reason for hiding this comment

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

Adjusted

Copy link
Member

@jbrockmendel jbrockmendel left a comment

Choose a reason for hiding this comment

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

LGTM thanks for being patient

@phofl phofl added this to the 2.0 milestone Jan 24, 2023
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.

Thanks. Could use a whatsnew note describing the performance improvement of indexing with masked dtypes

@phofl
Copy link
Member Author

phofl commented Jan 26, 2023

Added

@mroeschke mroeschke merged commit 7454934 into pandas-dev:main Jan 26, 2023
@mroeschke
Copy link
Member

Thanks @phofl

@jbrockmendel
Copy link
Member

Nice! Way to press through

@phofl phofl deleted the enh_masked_engine branch January 27, 2023 00:37
@phofl
Copy link
Member Author

phofl commented Jan 27, 2023

Thanks!

I'll work on deduplicating some parts of the code soonish (as discussed sometime during the review)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants