-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
PERF: Improve performance of Series.isin() on sets #25812
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,9 +21,9 @@ | |
is_complex_dtype, is_datetime64_any_dtype, is_datetime64tz_dtype, | ||
is_datetimelike, is_extension_array_dtype, is_float_dtype, is_integer, | ||
is_integer_dtype, is_interval_dtype, is_list_like, is_numeric_dtype, | ||
is_object_dtype, is_period_dtype, is_scalar, is_signed_integer_dtype, | ||
is_sparse, is_timedelta64_dtype, is_unsigned_integer_dtype, | ||
needs_i8_conversion) | ||
is_object_dtype, is_period_dtype, is_scalar, is_set_like, | ||
is_signed_integer_dtype, is_sparse, is_timedelta64_dtype, | ||
is_unsigned_integer_dtype, needs_i8_conversion) | ||
from pandas.core.dtypes.generic import ABCIndex, ABCIndexClass, ABCSeries | ||
from pandas.core.dtypes.missing import isna, na_value_for_dtype | ||
|
||
|
@@ -395,6 +395,14 @@ def isin(comps, values): | |
" to isin(), you passed a [{values_type}]" | ||
.format(values_type=type(values).__name__)) | ||
|
||
# GH 25507 | ||
# if `values` is a set, directly use it instead of hashing a list | ||
if is_set_like(values): | ||
result = np.empty_like(comps, dtype=np.bool) | ||
for i, comp in enumerate(comps): | ||
result[i] = comp in values | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for each test in test_algorithms for isin, add an arg that also is a set (for as many tests as possible) |
||
return result | ||
|
||
if not isinstance(values, (ABCIndex, ABCSeries, np.ndarray)): | ||
values = construct_1d_object_array_from_listlike(list(values)) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like you to integrate this with the other impl., meaning skip the set-ifying if its a set, but otherwise dispatch the actual isin operation. I suspect this will work only in some of the tests cases (but we aren't fully testing it here).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean by the other implementation or the actual isin operator. Do you mean let the rest of this function run but without the type cast? Or attempt to call comps.isin? It seems that would fail if comps was an ndarray.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what i mean is you can skip the list-ifying if its a set, but don't actually do the comp in values. as I said I suspect this actually doesn't work (some tests are failing) and if we add sets to a fair number of tests they will fail. The reason is that these all must be the same types exactly e.g. comp is often a int while values maybe be an np.int. we already fully handle this path, so you need to fit in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests that failed are unrelated to my changes (conda connection issues on setup). I will add more tests for set values, but dispatching the current path of isin will destroy the performance gain this change intended to fix.
I can't find any situations which cause this change to fail. The int vs np.int works fine, and I can create tests around such cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are creating a new path which makes the codes more complex ; we already have too many paths here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay then feel free to close this pull request; the current path cannot support this performance change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as I said this needs to integrate with the existing isin tests. this needs to happen before this patch is considered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the new patch works well for every test case except NaN types. The current paths ensure that an array containing NaN when compared against a collection with NaN in it returns True. However using Python sets NaN != NaN so it will return False (not sure of the reasoning, but I think current implementation goes against IEEE).
This means that
.isin({np.nan})
and.isin([np.nan])
will have different results, and I don't know of a way to resolve this without introducing an O(n) check that destroys the performance fix.Closing this pull request as I don't think we can implement this performance change without this regression.