Skip to content

Commit e5c7543

Browse files
authored
BUG: isin casting to float64 for unsigned int and list (#46693)
1 parent 27135a5 commit e5c7543

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ Indexing
825825
- Bug in :meth:`Series.__setitem__` with a non-integer :class:`Index` when using an integer key to set a value that cannot be set inplace where a ``ValueError`` was raised instead of casting to a common dtype (:issue:`45070`)
826826
- Bug in :meth:`Series.__setitem__` when setting incompatible values into a ``PeriodDtype`` or ``IntervalDtype`` :class:`Series` raising when indexing with a boolean mask but coercing when indexing with otherwise-equivalent indexers; these now consistently coerce, along with :meth:`Series.mask` and :meth:`Series.where` (:issue:`45768`)
827827
- Bug in :meth:`DataFrame.where` with multiple columns with datetime-like dtypes failing to downcast results consistent with other dtypes (:issue:`45837`)
828+
- Bug in :func:`isin` upcasting to ``float64`` with unsigned integer dtype and list-like argument without a dtype (:issue:`46485`)
828829
- Bug in :meth:`Series.loc.__setitem__` and :meth:`Series.loc.__getitem__` not raising when using multiple keys without using a :class:`MultiIndex` (:issue:`13831`)
829830
- Bug in :meth:`Index.reindex` raising ``AssertionError`` when ``level`` was specified but no :class:`MultiIndex` was given; level is ignored now (:issue:`35132`)
830831
- Bug when setting a value too large for a :class:`Series` dtype failing to coerce to a common type (:issue:`26049`, :issue:`32878`)

pandas/core/algorithms.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
is_numeric_dtype,
5858
is_object_dtype,
5959
is_scalar,
60+
is_signed_integer_dtype,
6061
is_timedelta64_dtype,
6162
needs_i8_conversion,
6263
)
@@ -446,7 +447,12 @@ def isin(comps: AnyArrayLike, values: AnyArrayLike) -> npt.NDArray[np.bool_]:
446447
)
447448

448449
if not isinstance(values, (ABCIndex, ABCSeries, ABCExtensionArray, np.ndarray)):
449-
values = _ensure_arraylike(list(values))
450+
if not is_signed_integer_dtype(comps):
451+
# GH#46485 Use object to avoid upcast to float64 later
452+
# TODO: Share with _find_common_type_compat
453+
values = construct_1d_object_array_from_listlike(list(values))
454+
else:
455+
values = _ensure_arraylike(list(values))
450456
elif isinstance(values, ABCMultiIndex):
451457
# Avoid raising in extract_array
452458
values = np.array(values)

pandas/tests/test_algos.py

+7
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,13 @@ def test_isin_float_df_string_search(self):
10891089
expected_false = DataFrame({"values": [False, False]})
10901090
tm.assert_frame_equal(result, expected_false)
10911091

1092+
def test_isin_unsigned_dtype(self):
1093+
# GH#46485
1094+
ser = Series([1378774140726870442], dtype=np.uint64)
1095+
result = ser.isin([1378774140726870528])
1096+
expected = Series(False)
1097+
tm.assert_series_equal(result, expected)
1098+
10921099

10931100
class TestValueCounts:
10941101
def test_value_counts(self):

0 commit comments

Comments
 (0)