Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: series with complex nan #53682

Merged
merged 5 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -511,6 +511,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)