8000 REGR: Fix bug when replacing categorical value with self by dsaxton · Pull Request #33292 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

REGR: Fix bug when replacing categorical value with self #33292

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

Merged
merged 6 commits into from
Apr 6, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Check earlier
  • Loading branch information
dsaxton committed Apr 5, 2020
commit 9be0d1bd78ae3d622a4656f14bf6612b2108d36e
5 changes: 3 additions & 2 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2447,6 +2447,8 @@ def replace(self, to_replace, value, inplace: bool = False):
# other cases, like if both to_replace and value are list-like or if
# to_replace is a dict, are handled separately in NDFrame
for replace_value, new_value in replace_dict.items():
if new_value == replace_value:
continue
if replace_value in cat.categories:
if isna(new_value):
cat.remove_categories(replace_value, inplace=True)
Expand All @@ -2456,8 +2458,7 @@ def replace(self, to_replace, value, inplace: bool = False):
if new_value in cat.categories:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can move this check to line 2451, e.g.

elif replace_value == new_value:
    continue

value_index = categories.index(new_value)
cat._codes[cat._codes == index] = value_index
if new_value != replace_value:
cat.remove_categories(replace_value, inplace=True)
cat.remove_categories(replace_value, inplace=True)
else:
categories[index] = new_value
cat.rename_categories(categories, inplace=True)
Expand Down
0