-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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: Series.replace does not preserve dtype of original Series #37512
Conversation
pandas/core/internals/blocks.py
Outdated
@@ -1174,6 +1177,9 @@ def coerce_to_target_dtype(self, other): | |||
f"possible recursion in coerce_to_target_dtype: {self} {other}" | |||
) | |||
|
|||
if is_categorical_dtype(dtype) or self.is_datetime: |
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.
the is_datetime condition is already handled above, if for some reason you actually need to do this then do it above (and I am not really sure why you are trying to do this)
pandas/core/internals/blocks.py
Outdated
@@ -1130,7 +1130,10 @@ def coerce_to_target_dtype(self, other): | |||
if is_dtype_equal(self.dtype, dtype): | |||
return self | |||
|
|||
if self.is_bool or is_object_dtype(dtype) or is_bool_dtype(dtype): | |||
if is_extension_array_dtype(self.dtype) and not is_categorical_dtype(dtype): |
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.
this if/then is already very complicated, what is the intent here?
Coverting to draft while I debug |
@arw2019 the underlying problem (or one of them at least) is that ExtensionBlock._can_hold_element and ExtensionBlock.putmask are completely FUBAR. I suggest you try adding the two following methods to CategoricalBlock (taken from a branch I have trying to get rid of CategoricalBlock.replace and _replace_list)
and in coerce_to_target_dtype just after the is_dtype_equal(self.dtype, dtype) check:
This changes the behavior for a handful of tests causing Series/DataFrame.replace to return CategoricalDtype (with added categories) instead of object-dtype, so may not be quite what we want. But I'm pretty sure its in the right ballpark. |
Thanks!!! Pushed this, will see what fails Update: there are 7 test failures:
Unfortunately they're undesirable behavioral changes (e.g. replacing a categorical with non-identical categories no longer raises, etc.) xref #36226, November 2020 dev call Mothballing this to clear the queue |
cc @jorisvandenbossche the latest commit here is pretty much exactly the edits i was referring to re CategoricalBlock.replace. Only difference is I would also remove CategoricalBlock.replace and _replace_list. |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff
Reheating #33622 (stale)