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 10 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
22 changes: 10 additions & 12 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,16 +506,6 @@ 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)
):
# 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)

elif isinstance(values, ABCMultiIndex):
# Avoid raising in extract_array
values = np.array(values)
Expand Down Expand Up @@ -569,6 +557,16 @@ def f(c, v):

else:
common = np_find_common_type(values.dtype, comps_array.dtype)
if (
values.dtype.kind in "iu"
and comps_array.dtype.kind in "iu"
and common == np.float64
):
# GH#46485
# Let's np_find_common_type do the job and return float64
# when it cannot do otherwise with integers
# We replace it by an object
common = np.dtype("O")
values = values.astype(common, copy=False)
comps_array = comps_array.astype(common, copy=False)
f = htable.ismember
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