-
-
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
Conversation
pandas/core/algorithms.py
Outdated
@@ -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 isinstance(values, set): |
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 think we have an is_set_like (otherwise pls create one)
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 didn't find an existing one so I have added it.
Codecov Report
@@ Coverage Diff @@
## master #25812 +/- ##
==========================================
+ Coverage 91.27% 91.27% +<.01%
==========================================
Files 173 173
Lines 53002 53007 +5
==========================================
+ Hits 48375 48381 +6
+ Misses 4627 4626 -1
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #25812 +/- ##
==========================================
+ Coverage 91.3% 91.45% +0.14%
==========================================
Files 173 172 -1
Lines 53004 52899 -105
==========================================
- Hits 48397 48380 -17
+ Misses 4607 4519 -88
Continue to review full report at Codecov.
|
@@ -395,6 +395,14 @@ def isin(comps, values): | |||
" to isin(), you passed a [{values_type}]" | |||
.format(values_type=type(values).__name__)) | |||
|
|||
# GH 25507 |
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.
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 comment
The 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)
@@ -395,6 +395,14 @@ def isin(comps, values): | |||
" to isin(), you passed a [{values_type}]" | |||
.format(values_type=type(values).__name__)) | |||
|
|||
# GH 25507 |
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.
@@ -395,6 +395,14 @@ def isin(comps, values): | |||
" to isin(), you passed a [{values_type}]" | |||
.format(values_type=type(values).__name__)) | |||
|
|||
# GH 25507 |
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.
git diff upstream/master -u -- "*.py" | flake8 --diff
Issue #25507 pointed out that using
DataFrame.isin
orSeries.isin
on a set took extremely long due to the set being first converted to a list then back to a hash table to lookup values. This fix eliminates that conversion and simply uses the set to directly test membership.Sample code from issue:
Output:
There are already several unit tests covering this function on sets with different dtypes. Additionally, I added an additional asv benchmark for sets:
If the additional benchmark is overkill I can of course remove it.