From 61c3a6f0ef59f51f32d9b206e7c38e87d46be88c Mon Sep 17 00:00:00 2001 From: phofl Date: Fri, 8 Apr 2022 13:22:15 +0200 Subject: [PATCH 1/2] BUG: isin casting to float64 for unsigned int and list --- doc/source/whatsnew/v1.5.0.rst | 1 + pandas/core/algorithms.py | 7 ++++++- pandas/tests/test_algos.py | 7 +++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 4920622a15f3f..ab448234da062 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -519,6 +519,7 @@ Indexing - 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`) - 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`) - Bug in :meth:`DataFrame.where` with multiple columns with datetime-like dtypes failing to downcast results consistent with other dtypes (:issue:`45837`) +- Bug in :func:`isin` upcasting to ``float64`` with unsigned integer dtype and list-like argument without a dtype (:issue:`46485`) - Bug in :meth:`Series.loc.__setitem__` and :meth:`Series.loc.__getitem__` not raising when using multiple keys without using a :class:`MultiIndex` (:issue:`13831`) - Bug in :meth:`Index.reindex` raising ``AssertionError`` when ``level`` was specified but no :class:`MultiIndex` was given; level is ignored now (:issue:`35132`) - Bug when setting a value too large for a :class:`Series` dtype failing to coerce to a common type (:issue:`26049`, :issue:`32878`) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 6c1dfc4c0da72..0c28d23cceffa 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -57,6 +57,7 @@ is_numeric_dtype, is_object_dtype, is_scalar, + is_signed_integer_dtype, is_timedelta64_dtype, needs_i8_conversion, ) @@ -446,7 +447,11 @@ def isin(comps: AnyArrayLike, values: AnyArrayLike) -> npt.NDArray[np.bool_]: ) if not isinstance(values, (ABCIndex, ABCSeries, ABCExtensionArray, np.ndarray)): - values = _ensure_arraylike(list(values)) + if not is_signed_integer_dtype(comps): + # GH#46485 Use object to avoid upcast to float64 later + values = construct_1d_object_array_from_listlike(list(values)) + else: + values = _ensure_arraylike(list(values)) elif isinstance(values, ABCMultiIndex): # Avoid raising in extract_array values = np.array(values) diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 7c16d13e59d3a..d66a9cad56f49 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -1089,6 +1089,13 @@ def test_isin_float_df_string_search(self): expected_false = DataFrame({"values": [False, False]}) tm.assert_frame_equal(result, expected_false) + def test_isin_unsigned_dtype(self): + # GH#46485 + ser = Series([1378774140726870442], dtype=np.uint64) + result = ser.isin([1378774140726870528]) + expected = Series(False) + tm.assert_series_equal(result, expected) + class TestValueCounts: def test_value_counts(self): From ac1e777c92238fddf49ca7566b4df405c707995c Mon Sep 17 00:00:00 2001 From: phofl Date: Wed, 15 Jun 2022 10:05:36 +0200 Subject: [PATCH 2/2] Add todo --- pandas/core/algorithms.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index ca9fa184ba5eb..38483808b1045 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -449,6 +449,7 @@ def isin(comps: AnyArrayLike, values: AnyArrayLike) -> npt.NDArray[np.bool_]: if not isinstance(values, (ABCIndex, ABCSeries, ABCExtensionArray, np.ndarray)): if not is_signed_integer_dtype(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(list(values)) else: values = _ensure_arraylike(list(values))