-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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 and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: Fix inconsistency of converting empty categorical with dtype_backend='pyarrow' #61131
Conversation
…pe_backend results in error. Since conversion of non-empty categorical returns categorical of 'numpy_nullable' dtype_backend, now, instead of raising an error, we ensure empty categorical is returned as well.
pandas/core/dtypes/cast.py
Outdated
@@ -1143,6 +1143,7 @@ def convert_dtypes( | |||
base_dtype.kind == "O" # type: ignore[union-attr] | |||
and input_array.size > 0 | |||
and isna(input_array).all() | |||
and not isinstance(input_array.dtype, CategoricalDtype) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Could you put this check above the isna(...).all()
check as it's less expensive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, could you properly add an elif isinstance(inferred_dtype, CategoricalDtype)
clause for this type in the above if/else
branch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this to the outer if branch, to skip the arrow dtype conversion for CategoricalDtype.
As mentioned in #59934, the expected behavior of convert_dtypes
for CategoricalDtype series essentially ignores the requested pyarrow backend conversion.
Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
Thanks @yuanx749 |
Follows up #59935
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.