BUG: Fix convert_dtype complex by Ma-gi-cian · Pull Request #62960 · pandas-dev/pandas (original) (raw)
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})
- closes BUG: allow complex type in convert_dtypes #60129
- Tests added and passed if fixing a bug or adding a new feature
- All code checks passed.
- Added an entry in the latest
doc/source/whatsnew/v3.0.0.rstfile if fixing a bug or adding a new feature.
Changes :
- Modified
pandas/core/dtypes/cast.pyto preserve complex dtypes (kind 'c') instead of attempting to convert them - Since there is no Numpy-nullable complex array nor a PyArrow complex dtype, this is effectively a no-op (leaves complex data as-is)
- Removed 'c' (complex) from dtype kind checks where it was incorrectly grouped with floats
- Added tests for both
DataFrame.convert_dtypes()andSeries.convert_dtypes()with complex data
Example script and output :
import pandas as pd print(pd.DataFrame(['1.0+5j', '1.5-3j']).astype(complex).convert_dtypes())
Output :
(pandas-dev) jha@MAGICIAN:~/workspace/opensource/reports/pandas-60129$ python3 main.py 0 0 1.0+5.0j 1 1.5-3.0j
Hi @rhshadrach
Could you review this PR and suggest on any changes if required. I've added the fix as specified - convert_dtypes() now no-ops for complex dtypes.
Thanks
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good.
Fixed the assert check - should pass all ci now
import pandas as pd
df = pd.DataFrame({"a": [1.0+5.0j, 1.5 - 3.0j], "b": [1,2]}) print(df) print("\n") print(df.dtypes)
a b0 1.0+5.0j 1 1 1.5-3.0j 2
a complex128 b int64 dtype: object
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
rhshadrach changed the title
Fix convert_dtype complex BUG: Fix convert_dtype complex
mittal-aakriti pushed a commit to mittal-aakriti/pandas that referenced this pull request
mittal-aakriti pushed a commit to mittal-aakriti/pandas that referenced this pull request