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: numpy.isin
does not function correctly with two arrays with different integer type
#22877
Comments
NumPy version 1.20.3 |
@nkit-chahal It's a new problem in version 1.24. |
@MilesCranmer do you have time to have a quick look. This looks like it must be related to gh-12065 misfiring. |
Sure, I have time to look at this today. |
@MilesCranmer After some research I think the problem might be in this line: numpy/numpy/lib/arraysetops.py Line 683 in 2b9851b
If I cannot think about an easy solution here. We can convert |
Quick question for @seberg: is this behaviour actually incorrect? In other words, do we want |
The behavior before version 1.24 is |
@TonyXiang8787 I think the solution should be implemented by correcting the overflow detection here: numpy/numpy/lib/arraysetops.py Line 652 in 2b9851b
To include information about the type of |
Yep this looks like the solution: - range_safe_from_overflow = ar2_range < np.iinfo(ar2.dtype).max
+ range_safe_from_overflow = ar2_range < np.iinfo(ar2.dtype).max and (ar1_max - ar2_min) < np.iinfo(ar1.dtype).max and (ar1_min - ar2_max) > np.iinfo(ar1.dtype).min I can make a PR when I get into the office, double check this fixes the bug, and also include some new unit tests. |
PR created on #22878. Thanks for raising this issue @TonyXiang8787. |
…2878) * TST: Mixed integer types for in1d * BUG: Fix mixed dtype overflows for in1d (#22877) * BUG: Type conversion for integer overflow check * MAINT: Fix linting issues in in1d * MAINT: ar1 overflow check only for non-empty array * MAINT: Expand bounds of overflow check * TST: Fix integer overflow in mixed boolean test * TST: Include test for overflow on mixed dtypes * MAINT: Less conservative overflow checks
numpy#22878) * TST: Mixed integer types for in1d * BUG: Fix mixed dtype overflows for in1d (numpy#22877) * BUG: Type conversion for integer overflow check * MAINT: Fix linting issues in in1d * MAINT: ar1 overflow check only for non-empty array * MAINT: Expand bounds of overflow check * TST: Fix integer overflow in mixed boolean test * TST: Include test for overflow on mixed dtypes * MAINT: Less conservative overflow checks
BUG: Fix integer overflow in in1d for mixed integer dtypes #22877
Describe the issue:
The function
numpy.isin
sometimes returns the wrong answer, if the integer type of the two arrays (to be compared) are not the same.The example below shows a
int8
array with one zero and aint64
array with two values. It should return oneTrue
. However, it returnsFalse
.Reproduce the code example:
Error message:
No response
Runtime information:
Numpy version
Sys version
Numpy runtime
Context for the issue:
The problem seems to only occur in version 1.24.
The text was updated successfully, but these errors were encountered: