Skip to content

BUG: Fix implicit conversion to float64 with isin() #61679

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
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,13 @@
is_bool_dtype,
is_complex_dtype,
is_dict_like,
is_dtype_equal,
is_extension_array_dtype,
is_float,
is_float_dtype,
is_integer,
is_integer_dtype,
is_list_like,
is_object_dtype,
is_signed_integer_dtype,
needs_i8_conversion,
)
from pandas.core.dtypes.concat import concat_compat
Expand Down Expand Up @@ -508,12 +506,26 @@ def isin(comps: ListLike, values: ListLike) -> npt.NDArray[np.bool_]:
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)
):
try:
src = comps.dtype # type: ignore[union-attr]
tar = values.dtype
# check only valid dtypes related to implicit conversion to float64
# other data types derived from 64-bit integers such as U/Int64Dtype
# should also work
if (
src.kind in "iu"
and src.itemsize == 8 # type: ignore[union-attr]
and tar.kind in "iu"
and tar.itemsize == 8 # type: ignore[union-attr]
):
is_implicit_conversion_to_float64 = src != tar
else:
is_implicit_conversion_to_float64 = False
except (TypeError, AttributeError, ImportError):
# invalid comparison
is_implicit_conversion_to_float64 = False

if is_implicit_conversion_to_float64:
# 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)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,13 @@ def test_isin_unsigned_dtype(self):
expected = Series(False)
tm.assert_series_equal(result, expected)

def test_isin_unsigned_dtype_other_side(self):
# GH#46485
ser = Series([1378774140726870442], dtype=np.int64)
result = ser.isin([np.uint64(1378774140726870528)])
expected = Series(False)
tm.assert_series_equal(result, expected)


class TestValueCounts:
def test_value_counts(self):
Expand Down
Loading