Skip to content

Commit c459d90

Browse files
Backport PR #33292 on branch 1.0.x (REGR: Fix bug when replacing categorical value with self) (#34004)
Co-authored-by: Daniel Saxton <[email protected]>
1 parent 1f87931 commit c459d90

File tree

3 files changed

+5
-0
lines changed

3 files changed

+5
-0
lines changed

doc/source/whatsnew/v1.0.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Fixed regressions
1717
~~~~~~~~~~~~~~~~~
1818
- Bug in :meth:`GroupBy.first` and :meth:`GroupBy.last` where None is not preserved in object dtype (:issue:`32800`)
1919
- Bug in DataFrame reductions using ``numeric_only=True`` and ExtensionArrays (:issue:`33256`).
20+
- Bug where :meth:`Categorical.replace` would replace with ``NaN`` whenever the new value and replacement value were equal (:issue:`33288`)
2021
-
2122

2223
.. _whatsnew_104.bug_fixes:

pandas/core/arrays/categorical.py

+2
Original file line numberDiff line numberDiff line change
@@ -2459,6 +2459,8 @@ def replace(self, to_replace, value, inplace: bool = False):
24592459
# other cases, like if both to_replace and value are list-like or if
24602460
# to_replace is a dict, are handled separately in NDFrame
24612461
for replace_value, new_value in replace_dict.items():
2462+
if new_value == replace_value:
2463+
continue
24622464
if replace_value in cat.categories:
24632465
if isna(new_value):
24642466
cat.remove_categories(replace_value, inplace=True)

pandas/tests/arrays/categorical/test_algos.py

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ def test_isin_cats():
6464
[
6565
("b", "c", ["a", "c"], "Categorical.categories are different"),
6666
("c", "d", ["a", "b"], None),
67+
# https://github.com/pandas-dev/pandas/issues/33288
68+
("a", "a", ["a", "b"], None),
6769
("b", None, ["a", None], "Categorical.categories length are different"),
6870
],
6971
)

0 commit comments

Comments
 (0)