Skip to content

Commit 89cb139

Browse files
authored
BUG: series with complex nan (#53682)
* avoid float(complex('nan')) * update * added test * modified tests; added changelog * changelog modified
1 parent 84d8069 commit 89cb139

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ Metadata
520520

521521
Other
522522
^^^^^
523+
- Bug in :class:`DataFrame` and :class:`Series` raising for data of complex dtype when ``NaN`` values are present (:issue:`53627`)
523524
- Bug in :class:`FloatingArray.__contains__` with ``NaN`` item incorrectly returning ``False`` when ``NaN`` values are present (:issue:`52840`)
524525
- Bug in :func:`api.interchange.from_dataframe` when converting an empty DataFrame object (:issue:`53155`)
525526
- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`)

pandas/_libs/lib.pyx

+8-1
Original file line numberDiff line numberDiff line change
@@ -2485,7 +2485,14 @@ def maybe_convert_objects(ndarray[object] objects,
24852485
elif util.is_nan(val):
24862486
seen.nan_ = True
24872487
mask[i] = True
2488-
floats[i] = complexes[i] = val
2488+
if util.is_complex_object(val):
2489+
floats[i] = fnan
2490+
complexes[i] = val
2491+
seen.complex_ = True
2492+
if not convert_numeric:
2493+
break
2494+
else:
2495+
floats[i] = complexes[i] = val
24892496
elif util.is_bool_object(val):
24902497
seen.bool_ = True
24912498
bools[i] = val

pandas/tests/series/test_constructors.py

+15
Original file line numberDiff line numberDiff line change
@@ -2154,3 +2154,18 @@ def test_index_ordered_dict_keys():
21542154
),
21552155
)
21562156
tm.assert_series_equal(series, expected)
2157+
2158+
2159+
@pytest.mark.parametrize(
2160+
"input_list",
2161+
[
2162+
[1, complex("nan"), 2],
2163+
[1 + 1j, complex("nan"), 2 + 2j],
2164+
],
2165+
)
2166+
def test_series_with_complex_nan(input_list):
2167+
# GH#53627
2168+
ser = Series(input_list)
2169+
result = Series(ser.array)
2170+
assert ser.dtype == "complex128"
2171+
tm.assert_series_equal(ser, result)

0 commit comments

Comments
 (0)