Skip to content

Commit 39b7a6d

Browse files
authored
BUG: setitem into complex64 overflow (#45285)
1 parent c80f656 commit 39b7a6d

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

doc/source/whatsnew/v1.5.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ Indexing
203203
- Bug in :meth:`loc.__getitem__` with a list of keys causing an internal inconsistency that could lead to a disconnect between ``frame.at[x, y]`` vs ``frame[y].loc[x]`` (:issue:`22372`)
204204
- 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`)
205205
- 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`)
206-
- Bug when setting an integer too large for a :class:`Series` dtype failing to coerce to a common type (:issue:`26049`)
206+
- Bug when setting a value too large for a :class:`Series` dtype failing to coerce to a common type (:issue:`26049`, :issue:`32878`)
207207
-
208208

209209
Missing

pandas/core/dtypes/cast.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -2023,12 +2023,21 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any:
20232023
raise ValueError
20242024

20252025
elif dtype.kind == "c":
2026+
if lib.is_integer(element) or lib.is_complex(element) or lib.is_float(element):
2027+
if np.isnan(element):
2028+
# see test_where_complex GH#6345
2029+
return dtype.type(element)
2030+
2031+
casted = dtype.type(element)
2032+
if casted == element:
2033+
return casted
2034+
# otherwise e.g. overflow see test_32878_complex_itemsize
2035+
raise ValueError
2036+
20262037
if tipo is not None:
20272038
if tipo.kind in ["c", "f", "i", "u"]:
20282039
return element
20292040
raise ValueError
2030-
if lib.is_integer(element) or lib.is_complex(element) or lib.is_float(element):
2031-
return element
20322041
raise ValueError
20332042

20342043
elif dtype.kind == "b":

pandas/tests/series/indexing/test_setitem.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,19 @@ def test_32878_int_itemsize():
13461346
tm.assert_series_equal(ser, expected)
13471347

13481348

1349+
def test_32878_complex_itemsize():
1350+
arr = np.arange(5).astype("c8")
1351+
ser = Series(arr)
1352+
val = np.finfo(np.float64).max
1353+
val = val.astype("c16")
1354+
1355+
# GH#32878 used to coerce val to inf+0.000000e+00j
1356+
ser[0] = val
1357+
assert ser[0] == val
1358+
expected = Series([val, 1, 2, 3, 4], dtype="c16")
1359+
tm.assert_series_equal(ser, expected)
1360+
1361+
13491362
def test_37692(indexer_al):
13501363
# GH#37692
13511364
ser = Series([1, 2, 3], index=["a", "b", "c"])
@@ -1397,21 +1410,6 @@ def test_setitem_positional_float_into_int_coerces():
13971410
tm.assert_series_equal(ser, expected)
13981411

13991412

1400-
@pytest.mark.xfail(reason="Fails to upcast")
1401-
def test_32878_complex_itemsize():
1402-
# TODO: when fixed, put adjacent to test_32878_int_itemsize
1403-
arr = np.arange(5).astype("c8")
1404-
ser = Series(arr)
1405-
val = np.finfo(np.float64).max
1406-
val = val.astype("c16")
1407-
1408-
# GH#32878 used to coerce val to inf+0.000000e+00j
1409-
ser[0] = val
1410-
assert ser[0] == val
1411-
expected = Series([val, 1, 2, 3, 4], dtype="c16")
1412-
tm.assert_series_equal(ser, expected)
1413-
1414-
14151413
def test_setitem_int_as_positional_fallback_deprecation():
14161414
# GH#42215 deprecated falling back to positional on __setitem__ with an
14171415
# int not contained in the index

0 commit comments

Comments
 (0)