From e1c32f082c43ec7952eca72530281c3b195c2639 Mon Sep 17 00:00:00 2001 From: Aditya Jha Date: Sun, 2 Nov 2025 14:22:29 +0000 Subject: [PATCH 1/2] Fix convert_dtype complex --- pandas/core/dtypes/cast.py | 12 ++++++++---- pandas/tests/frame/methods/test_convert_dtypes.py | 7 +++++++ pandas/tests/series/methods/test_convert_dtypes.py | 7 +++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 3b615c70ebea2..391cbc3ffad43 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -934,6 +934,10 @@ def convert_dtypes( if ( convert_string or convert_integer or convert_boolean or convert_floating ) and isinstance(input_array, np.ndarray): + + if input_array.dtype.kind == 'c': + return input_array.dtype + if input_array.dtype == object: inferred_dtype = lib.infer_dtype(input_array) else: @@ -954,7 +958,7 @@ def convert_dtypes( inferred_dtype = NUMPY_INT_TO_DTYPE.get( input_array.dtype, target_int_dtype ) - elif input_array.dtype.kind in "fcb": + elif input_array.dtype.kind in "fb": # TODO: de-dup with maybe_cast_to_integer_array? arr = input_array[notna(input_array)] if len(arr) < len(input_array) and not is_nan_na(): @@ -972,7 +976,7 @@ def convert_dtypes( inferred_dtype = target_int_dtype if convert_floating: - if input_array.dtype.kind in "fcb": + if input_array.dtype.kind in "fb": # i.e. numeric but not integer from pandas.core.arrays.floating import NUMPY_FLOAT_TO_DTYPE @@ -1028,11 +1032,11 @@ def convert_dtypes( if ( (convert_integer and inferred_dtype.kind in "iu") - or (convert_floating and inferred_dtype.kind in "fc") + or (convert_floating and inferred_dtype.kind in "f") or (convert_boolean and inferred_dtype.kind == "b") or (convert_string and isinstance(inferred_dtype, StringDtype)) or ( - inferred_dtype.kind not in "iufcb" + inferred_dtype.kind not in "iufb" and not isinstance(inferred_dtype, StringDtype) and not isinstance(inferred_dtype, CategoricalDtype) ) diff --git a/pandas/tests/frame/methods/test_convert_dtypes.py b/pandas/tests/frame/methods/test_convert_dtypes.py index e90786a43c483..1d6235591cf17 100644 --- a/pandas/tests/frame/methods/test_convert_dtypes.py +++ b/pandas/tests/frame/methods/test_convert_dtypes.py @@ -228,3 +228,10 @@ def test_convert_dtype_pyarrow_timezone_preserve(self): result = df.convert_dtypes(dtype_backend="pyarrow") expected = df.copy() tm.assert_frame_equal(result, expected) + + def test_convert_dtypes_complex(self): + # GH 60129 + df = pd.DataFrame({'a': [1.0+5.0j, 1.5-3.0j]}) + result = df.convert_dtypes() + tm.assert_frame_equal(result, df) + assert result['a'].dtype.kind == 'c' diff --git a/pandas/tests/series/methods/test_convert_dtypes.py b/pandas/tests/series/methods/test_convert_dtypes.py index 57d0c60118504..8621950e5f6e6 100644 --- a/pandas/tests/series/methods/test_convert_dtypes.py +++ b/pandas/tests/series/methods/test_convert_dtypes.py @@ -332,3 +332,10 @@ def test_convert_dtype_pyarrow_timezone_preserve(self): result = ser.convert_dtypes(dtype_backend="pyarrow") expected = ser.copy() tm.assert_series_equal(result, expected) + + def test_convert_dtypes_complex(self): + # GH 60129 + ser = pd.Series([1.5+3.0j, 1.5-3.0j]) + result = ser.convert_dtypes() + tm.assert_series_equal(result, ser) + assert result.dtype.kind == 'c' From 8a427fcf5fceefa5e1b1ae54babf54e3a95946d9 Mon Sep 17 00:00:00 2001 From: Aditya Jha Date: Sun, 2 Nov 2025 17:37:49 +0000 Subject: [PATCH 2/2] Added whatsnew and ran pre-commit --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/core/dtypes/cast.py | 3 +-- pandas/tests/frame/methods/test_convert_dtypes.py | 4 ++-- pandas/tests/series/methods/test_convert_dtypes.py | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 12f522301e121..cfa5b98920402 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -1037,6 +1037,7 @@ Conversion - Bug in :meth:`DataFrame.astype` not casting ``values`` for Arrow-based dictionary dtype correctly (:issue:`58479`) - Bug in :meth:`DataFrame.update` bool dtype being converted to object (:issue:`55509`) - Bug in :meth:`Series.astype` might modify read-only array inplace when casting to a string dtype (:issue:`57212`) +- Bug in :meth:`Series.convert_dtypes` and :meth:`DataFrame.convert_dtypes` raising ``TypeError`` when called on data with complex dtype (:issue:`60129`) - Bug in :meth:`Series.convert_dtypes` and :meth:`DataFrame.convert_dtypes` removing timezone information for objects with :class:`ArrowDtype` (:issue:`60237`) - Bug in :meth:`Series.reindex` not maintaining ``float32`` type when a ``reindex`` introduces a missing value (:issue:`45857`) - Bug in :meth:`to_datetime` and :meth:`to_timedelta` with input ``None`` returning ``None`` instead of ``NaT``, inconsistent with other conversion methods (:issue:`23055`) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 391cbc3ffad43..6ff80a0ffc790 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -934,8 +934,7 @@ def convert_dtypes( if ( convert_string or convert_integer or convert_boolean or convert_floating ) and isinstance(input_array, np.ndarray): - - if input_array.dtype.kind == 'c': + if input_array.dtype.kind == "c": return input_array.dtype if input_array.dtype == object: diff --git a/pandas/tests/frame/methods/test_convert_dtypes.py b/pandas/tests/frame/methods/test_convert_dtypes.py index 1d6235591cf17..d7531af4a976d 100644 --- a/pandas/tests/frame/methods/test_convert_dtypes.py +++ b/pandas/tests/frame/methods/test_convert_dtypes.py @@ -231,7 +231,7 @@ def test_convert_dtype_pyarrow_timezone_preserve(self): def test_convert_dtypes_complex(self): # GH 60129 - df = pd.DataFrame({'a': [1.0+5.0j, 1.5-3.0j]}) + df = pd.DataFrame({"a": [1.0 + 5.0j, 1.5 - 3.0j]}) result = df.convert_dtypes() tm.assert_frame_equal(result, df) - assert result['a'].dtype.kind == 'c' + assert result["a"].dtype.kind == "c" diff --git a/pandas/tests/series/methods/test_convert_dtypes.py b/pandas/tests/series/methods/test_convert_dtypes.py index 8621950e5f6e6..5a859132ee914 100644 --- a/pandas/tests/series/methods/test_convert_dtypes.py +++ b/pandas/tests/series/methods/test_convert_dtypes.py @@ -335,7 +335,7 @@ def test_convert_dtype_pyarrow_timezone_preserve(self): def test_convert_dtypes_complex(self): # GH 60129 - ser = pd.Series([1.5+3.0j, 1.5-3.0j]) + ser = pd.Series([1.5 + 3.0j, 1.5 - 3.0j]) result = ser.convert_dtypes() tm.assert_series_equal(result, ser) - assert result.dtype.kind == 'c' + assert result.dtype.kind == "c"