From 9bf4dbe9049a78655d2ca1d618df83ec9b378d18 Mon Sep 17 00:00:00 2001 From: Charlie-XIAO Date: Thu, 15 Jun 2023 13:25:50 +0800 Subject: [PATCH 1/5] avoid float(complex('nan')) --- pandas/_libs/lib.pyx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index f7934865fbb43..bb287b74799b2 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -2485,7 +2485,8 @@ def maybe_convert_objects(ndarray[object] objects, elif util.is_nan(val): seen.nan_ = True mask[i] = True - floats[i] = complexes[i] = val + floats[i] = fnan + complexes[i] = val elif util.is_bool_object(val): seen.bool_ = True bools[i] = val From adfca17feb2b512c01d1c8085f02c27d3d94f6aa Mon Sep 17 00:00:00 2001 From: Charlie-XIAO Date: Thu, 15 Jun 2023 17:08:38 +0800 Subject: [PATCH 2/5] update --- pandas/_libs/lib.pyx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index bb287b74799b2..a622de742a840 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -2485,8 +2485,14 @@ def maybe_convert_objects(ndarray[object] objects, elif util.is_nan(val): seen.nan_ = True mask[i] = True - floats[i] = fnan - 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 From 03fdff913d189f747ac64bad18cbb8a5c231936b Mon Sep 17 00:00:00 2001 From: Charlie-XIAO Date: Thu, 15 Jun 2023 17:15:16 +0800 Subject: [PATCH 3/5] added test --- pandas/tests/generic/test_series.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/tests/generic/test_series.py b/pandas/tests/generic/test_series.py index ee0a7fb77f336..35b20b3fbafb4 100644 --- a/pandas/tests/generic/test_series.py +++ b/pandas/tests/generic/test_series.py @@ -157,3 +157,8 @@ def finalize(self, other, method=None, **kwargs): result = pd.concat([ser, ser2]) assert result.filename == "foo+bar" assert result.name is None + + def test_series_with_complex_nan(self): + # GH#53627 + ser = Series([complex("nan")]) + assert np.isnan(ser[0].real) and ser[0].imag == 0 From add5986a58165b50fc23e5d95d841860eb121b0f Mon Sep 17 00:00:00 2001 From: Charlie-XIAO Date: Fri, 16 Jun 2023 00:38:33 +0800 Subject: [PATCH 4/5] modified tests; added changelog --- doc/source/whatsnew/v2.1.0.rst | 1 + pandas/tests/generic/test_series.py | 5 ----- pandas/tests/series/test_constructors.py | 15 +++++++++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 137be168985d2..8f2206fb6512c 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -512,6 +512,7 @@ Metadata Other ^^^^^ - Bug in :class:`FloatingArray.__contains__` with ``NaN`` item incorrectly returning ``False`` when ``NaN`` values are present (:issue:`52840`) +- Bug in :class:`Series` raising an error for data of complex dtype when ``NaN`` values are present (:issue:`53627`) - 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`) - Bug in :func:`assert_frame_equal` checks category dtypes even when asked not to check index type (:issue:`52126`) diff --git a/pandas/tests/generic/test_series.py b/pandas/tests/generic/test_series.py index 35b20b3fbafb4..ee0a7fb77f336 100644 --- a/pandas/tests/generic/test_series.py +++ b/pandas/tests/generic/test_series.py @@ -157,8 +157,3 @@ def finalize(self, other, method=None, **kwargs): result = pd.concat([ser, ser2]) assert result.filename == "foo+bar" assert result.name is None - - def test_series_with_complex_nan(self): - # GH#53627 - ser = Series([complex("nan")]) - assert np.isnan(ser[0].real) and ser[0].imag == 0 diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 4bf16b6d20d1f..ceb283ca9e9e7 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -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) From aac4d580125be4956ee4d3975430ca99305142cd Mon Sep 17 00:00:00 2001 From: Charlie-XIAO Date: Fri, 16 Jun 2023 00:47:30 +0800 Subject: [PATCH 5/5] changelog modified --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 8f2206fb6512c..0f246e58ed016 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -511,8 +511,8 @@ 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 :class:`Series` raising an error for data of complex dtype when ``NaN`` values are present (:issue:`53627`) - 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`) - Bug in :func:`assert_frame_equal` checks category dtypes even when asked not to check index type (:issue:`52126`)