Skip to content

Commit e1c32f0

Browse files
committed
Fix convert_dtype complex
1 parent 8f359f8 commit e1c32f0

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

pandas/core/dtypes/cast.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,10 @@ def convert_dtypes(
934934
if (
935935
convert_string or convert_integer or convert_boolean or convert_floating
936936
) and isinstance(input_array, np.ndarray):
937+
938+
if input_array.dtype.kind == 'c':
939+
return input_array.dtype
940+
937941
if input_array.dtype == object:
938942
inferred_dtype = lib.infer_dtype(input_array)
939943
else:
@@ -954,7 +958,7 @@ def convert_dtypes(
954958
inferred_dtype = NUMPY_INT_TO_DTYPE.get(
955959
input_array.dtype, target_int_dtype
956960
)
957-
elif input_array.dtype.kind in "fcb":
961+
elif input_array.dtype.kind in "fb":
958962
# TODO: de-dup with maybe_cast_to_integer_array?
959963
arr = input_array[notna(input_array)]
960964
if len(arr) < len(input_array) and not is_nan_na():
@@ -972,7 +976,7 @@ def convert_dtypes(
972976
inferred_dtype = target_int_dtype
973977

974978
if convert_floating:
975-
if input_array.dtype.kind in "fcb":
979+
if input_array.dtype.kind in "fb":
976980
# i.e. numeric but not integer
977981
from pandas.core.arrays.floating import NUMPY_FLOAT_TO_DTYPE
978982

@@ -1028,11 +1032,11 @@ def convert_dtypes(
10281032

10291033
if (
10301034
(convert_integer and inferred_dtype.kind in "iu")
1031-
or (convert_floating and inferred_dtype.kind in "fc")
1035+
or (convert_floating and inferred_dtype.kind in "f")
10321036
or (convert_boolean and inferred_dtype.kind == "b")
10331037
or (convert_string and isinstance(inferred_dtype, StringDtype))
10341038
or (
1035-
inferred_dtype.kind not in "iufcb"
1039+
inferred_dtype.kind not in "iufb"
10361040
and not isinstance(inferred_dtype, StringDtype)
10371041
and not isinstance(inferred_dtype, CategoricalDtype)
10381042
)

pandas/tests/frame/methods/test_convert_dtypes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,10 @@ def test_convert_dtype_pyarrow_timezone_preserve(self):
228228
result = df.convert_dtypes(dtype_backend="pyarrow")
229229
expected = df.copy()
230230
tm.assert_frame_equal(result, expected)
231+
232+
def test_convert_dtypes_complex(self):
233+
# GH 60129
234+
df = pd.DataFrame({'a': [1.0+5.0j, 1.5-3.0j]})
235+
result = df.convert_dtypes()
236+
tm.assert_frame_equal(result, df)
237+
assert result['a'].dtype.kind == 'c'

pandas/tests/series/methods/test_convert_dtypes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,10 @@ def test_convert_dtype_pyarrow_timezone_preserve(self):
332332
result = ser.convert_dtypes(dtype_backend="pyarrow")
333333
expected = ser.copy()
334334
tm.assert_series_equal(result, expected)
335+
336+
def test_convert_dtypes_complex(self):
337+
# GH 60129
338+
ser = pd.Series([1.5+3.0j, 1.5-3.0j])
339+
result = ser.convert_dtypes()
340+
tm.assert_series_equal(result, ser)
341+
assert result.dtype.kind == 'c'

0 commit comments

Comments
 (0)