From 0af3f2424d632c00331fff07671a1e622f23dc70 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 9 Jan 2022 09:15:25 -0800 Subject: [PATCH 1/2] BUG: setitem into complex64 overflow --- pandas/core/dtypes/cast.py | 13 +++++++-- pandas/tests/series/indexing/test_setitem.py | 28 +++++++++----------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 80271f04d4449..f509ba48e8d1c 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1936,12 +1936,21 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any: raise ValueError elif dtype.kind == "c": + if lib.is_integer(element) or lib.is_complex(element) or lib.is_float(element): + if np.isnan(element): + # see test_where_complex GH#6345 + return dtype.type(element) + + casted = dtype.type(element) + if casted == element: + return casted + # otherwise e.g. overflow see test_32878_complex_itemsize + raise ValueError + if tipo is not None: if tipo.kind in ["c", "f", "i", "u"]: return element raise ValueError - if lib.is_integer(element) or lib.is_complex(element) or lib.is_float(element): - return element raise ValueError elif dtype.kind == "b": diff --git a/pandas/tests/series/indexing/test_setitem.py b/pandas/tests/series/indexing/test_setitem.py index 94a5ca38afce4..a10e56a2aa170 100644 --- a/pandas/tests/series/indexing/test_setitem.py +++ b/pandas/tests/series/indexing/test_setitem.py @@ -1392,6 +1392,19 @@ def test_32878_int_itemsize(): tm.assert_series_equal(ser, expected) +def test_32878_complex_itemsize(): + arr = np.arange(5).astype("c8") + ser = Series(arr) + val = np.finfo(np.float64).max + val = val.astype("c16") + + # GH#32878 used to coerce val to inf+0.000000e+00j + ser[0] = val + assert ser[0] == val + expected = Series([val, 1, 2, 3, 4], dtype="c16") + tm.assert_series_equal(ser, expected) + + def test_26395(indexer_al): # .at case fixed by GH#45121 (best guess) df = DataFrame(index=["A", "B", "C"]) @@ -1492,21 +1505,6 @@ def test_15231(): tm.assert_series_equal(df.dtypes, exp_dtypes) -@pytest.mark.xfail(reason="Fails to upcast") -def test_32878_complex_itemsize(): - # TODO: when fixed, put adjacent to test_32878_int_itemsize - arr = np.arange(5).astype("c8") - ser = Series(arr) - val = np.finfo(np.float64).max - val = val.astype("c16") - - # GH#32878 used to coerce val to inf+0.000000e+00j - ser[0] = val - assert ser[0] == val - expected = Series([val, 1, 2, 3, 4], dtype="c16") - tm.assert_series_equal(ser, expected) - - @pytest.mark.xfail(reason="Unnecessarily upcasts to float64") def test_iloc_setitem_unnecesssary_float_upcasting(): # GH#12255 From faedaf6630be7fa6ef1715beb48cfbe6e818baa8 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 10 Jan 2022 08:28:53 -0800 Subject: [PATCH 2/2] whatsnew --- doc/source/whatsnew/v1.5.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 0173807cb9bd0..0e78e230f8ee4 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -154,7 +154,7 @@ Indexing ^^^^^^^^ - Bug in :meth:`DataFrame.iloc` where indexing a single row on a :class:`DataFrame` with a single ExtensionDtype column gave a copy instead of a view on the underlying data (:issue:`45241`) - 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 insead of casting to a common dtype (:issue:`45070`) -- Bug when setting an integer too large for a :class:`Series` dtype failing to coerce to a common type (:issue:`26049`) +- Bug when setting a value too large for a :class:`Series` dtype failing to coerce to a common type (:issue:`26049`, :issue:`32878`) - Missing