diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 177b1ccd166cb..741fc323262b6 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -2218,6 +2218,8 @@ def can_hold_element(arr: ArrayLike, element: Any) -> bool: if dtype.kind in ["i", "u"]: if tipo is not None: if tipo.kind not in ["i", "u"]: + if is_float(element) and element.is_integer(): + return True # Anything other than integer we cannot hold return False elif dtype.itemsize < tipo.itemsize: diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index c7769046c70b2..adca54abce04d 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -781,14 +781,6 @@ def _replace_list( # so un-tile here return self.replace(src_list, dest_list[0], inplace, regex) - # https://github.com/pandas-dev/pandas/issues/40371 - # the following pairs check code caused a regression so we catch that case here - # until the issue is fixed properly in can_hold_element - - # error: "Iterable[Any]" has no attribute "tolist" - if hasattr(src_list, "tolist"): - src_list = src_list.tolist() # type: ignore[attr-defined] - # Exclude anything that we know we won't contain pairs = [ (x, y) for x, y in zip(src_list, dest_list) if self._can_hold_element(x) diff --git a/pandas/tests/series/indexing/test_setitem.py b/pandas/tests/series/indexing/test_setitem.py index 3f850dfbc6a39..13054062defb4 100644 --- a/pandas/tests/series/indexing/test_setitem.py +++ b/pandas/tests/series/indexing/test_setitem.py @@ -158,7 +158,7 @@ def test_setitem_series_object_dtype(self, indexer, ser_index): expected = Series([Series([42], index=[ser_index]), 0], dtype="object") tm.assert_series_equal(ser, expected) - @pytest.mark.parametrize("index, exp_value", [(0, 42.0), (1, np.nan)]) + @pytest.mark.parametrize("index, exp_value", [(0, 42), (1, np.nan)]) def test_setitem_series(self, index, exp_value): # GH#38303 ser = Series([0, 0])