-
-
Notifications
You must be signed in to change notification settings - Fork 19.2k
BUG: avoid float upcast when mixing signed/unsigned ints in isin #62608
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
Open
divya1974
wants to merge
7
commits into
pandas-dev:main
Choose a base branch
from
divya1974:fix/61676-isin-signed-unsigned
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7350469
BUG: avoid float upcast when mixing signed/unsigned ints in isin (tes…
divya1974 6dc52f6
Merge branch 'main' into fix/61676-isin-signed-unsigned
divya1974 6b417e7
performance improved approach
divya1974 b65a9f2
fix tests
divya1974 13dcc7b
Merge branch 'main' into fix/61676-isin-signed-unsigned
divya1974 3a7487d
Clean up comments in test_isin_int64_vs_uint64_mismatch
divya1974 12a93fb
Refine comments on fast-path for integer itemsize
divya1974 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,7 +57,6 @@ | |
| is_integer_dtype, | ||
| is_list_like, | ||
| is_object_dtype, | ||
| is_signed_integer_dtype, | ||
| needs_i8_conversion, | ||
| ) | ||
| from pandas.core.dtypes.concat import concat_compat | ||
|
|
@@ -518,16 +517,10 @@ def isin(comps: ListLike, values: ListLike) -> npt.NDArray[np.bool_]: | |
| if not isinstance(values, (ABCIndex, ABCSeries, ABCExtensionArray, np.ndarray)): | ||
| orig_values = list(values) | ||
| values = _ensure_arraylike(orig_values, func_name="isin-targets") | ||
|
|
||
| if ( | ||
| len(values) > 0 | ||
| and values.dtype.kind in "iufcb" | ||
| and not is_signed_integer_dtype(comps) | ||
| and not is_dtype_equal(values, comps) | ||
| ): | ||
| # GH#46485 Use object to avoid upcast to float64 later | ||
| # TODO: Share with _find_common_type_compat | ||
| values = construct_1d_object_array_from_listlike(orig_values) | ||
| # Keep values as a numeric ndarray where possible; we handle | ||
| # signed/unsigned integer mixes with a fast-path later (after | ||
| # comps_array extraction) to avoid object-dtype conversions that | ||
| # harm performance for large numeric arrays. | ||
|
|
||
| elif isinstance(values, ABCMultiIndex): | ||
| # Avoid raising in extract_array | ||
|
|
@@ -579,6 +572,34 @@ def f(c, v): | |
| f = lambda a, b: np.isin(a, b).ravel() | ||
|
|
||
| else: | ||
| # Fast-path: handle integer-kind mixes without upcasting to float64. | ||
| if ( | ||
| values.dtype.kind in "iu" | ||
| and comps_array.dtype.kind in "iu" | ||
| # Only apply fast-path for 64-bit integer widths to avoid | ||
| # surprising behaviour on platforms or dtypes with different | ||
| # itemsize. Narrowing to 8-byte ints | ||
| # keeps the fast-path safe and performant for the common case. | ||
| and values.dtype.itemsize == 8 | ||
| and comps_array.dtype.itemsize == 8 | ||
| and not is_dtype_equal(values.dtype, comps_array.dtype) | ||
| ): | ||
| try: | ||
| if values.size > 0 and comps_array.size > 0: | ||
| signed_negative = False | ||
| if values.dtype.kind == "i": | ||
| signed_negative = values.min() < 0 | ||
| if comps_array.dtype.kind == "i": | ||
| signed_negative = signed_negative or (comps_array.min() < 0) | ||
|
|
||
| if not signed_negative: | ||
| values_u = values.astype("uint64", copy=False) | ||
| comps_u = comps_array.astype("uint64", copy=False) | ||
| return htable.ismember(comps_u, values_u) | ||
| except Exception: | ||
|
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. We almost never toss a base Exception in the code base. What is this trying to catch here? |
||
| # fall back to generic path on error | ||
| pass | ||
|
|
||
| common = np_find_common_type(values.dtype, comps_array.dtype) | ||
| values = values.astype(common, copy=False) | ||
| comps_array = comps_array.astype(common, copy=False) | ||
|
|
||
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
Oops, something went wrong.
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.
Can you expand on this comment? I don't fully understand what the surprising behavior is, and I'm worried its a red herring