pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.87k stars 18.02k forks source link

BUG: allow complex type in convert_dtypes #60129

Open yasirroni opened 3 weeks ago

yasirroni commented 3 weeks ago

Feature Type

Problem Description

convert_dtypes should be able to do this type of coercion from string complex looking

print(pd.DataFrame(['1.0+5j', '1.5-3j']).astype(complex).convert_dtypes())

Related to closed https://github.com/pandas-dev/pandas/issues/4895

Feature Description

-

Alternative Solutions

-

Additional Context

No response

rhshadrach commented 3 weeks ago

I'm positive on convert_dtypes supporting complex, currently it raises TypeError: complex128 cannot be converted to FloatingDtype if input has complex dtype. There is no Numpy-nullable complex array nor a PyArrow complex dtype, so for both backends this will be a noop (leaves complex data as-is).

convert_dtypes should be able to do this type of coercion from string complex looking

convert_dtypes does not convert strings to non-strings, so the current behavior of pd.DataFrame(['1.0+5j', '1.5-3j']).convert_dtypes() looks correct to me.