Skip to content
Open
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/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
11 changes: 7 additions & 4 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,9 @@ 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:
Expand All @@ -954,7 +957,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():
Expand All @@ -972,7 +975,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

Expand Down Expand Up @@ -1028,11 +1031,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)
)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/methods/test_convert_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
7 changes: 7 additions & 0 deletions pandas/tests/series/methods/test_convert_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading