Skip to content

Commit

Permalink
BUG: series with complex nan (pandas-dev#53682)
Browse files Browse the repository at this point in the history
* avoid float(complex('nan'))

* update

* added test

* modified tests; added changelog

* changelog modified
  • Loading branch information
Charlie-XIAO authored and im-vinicius committed Jul 8, 2023
1 parent b571deb commit e3ea4ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ Metadata

Other
^^^^^
- Bug in :class:`DataFrame` and :class:`Series` raising for data of complex dtype when ``NaN`` values are present (:issue:`53627`)
- Bug in :class:`FloatingArray.__contains__` with ``NaN`` item incorrectly returning ``False`` when ``NaN`` values are present (:issue:`52840`)
- Bug in :func:`api.interchange.from_dataframe` when converting an empty DataFrame object (:issue:`53155`)
- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`)
Expand Down
9 changes: 8 additions & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2485,7 +2485,14 @@ def maybe_convert_objects(ndarray[object] objects,
elif util.is_nan(val):
seen.nan_ = True
mask[i] = True
floats[i] = complexes[i] = val
if util.is_complex_object(val):
floats[i] = fnan
complexes[i] = val
seen.complex_ = True
if not convert_numeric:
break
else:
floats[i] = complexes[i] = val
elif util.is_bool_object(val):
seen.bool_ = True
bools[i] = val
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2154,3 +2154,18 @@ def test_index_ordered_dict_keys():
),
)
tm.assert_series_equal(series, expected)


@pytest.mark.parametrize(
"input_list",
[
[1, complex("nan"), 2],
[1 + 1j, complex("nan"), 2 + 2j],
],
)
def test_series_with_complex_nan(input_list):
# GH#53627
ser = Series(input_list)
result = Series(ser.array)
assert ser.dtype == "complex128"
tm.assert_series_equal(ser, result)

0 comments on commit e3ea4ff

Please sign in to comment.