Skip to content

Commit bffafbb

Browse files
authored
REGR: Fix bug when replacing categorical value with self (#33292)
1 parent 89d8aba commit bffafbb

File tree

3 files changed

+5
-0
lines changed

3 files changed

+5
-0
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ Categorical
294294
- Bug when passing categorical data to :class:`Index` constructor along with ``dtype=object`` incorrectly returning a :class:`CategoricalIndex` instead of object-dtype :class:`Index` (:issue:`32167`)
295295
- Bug where :class:`Categorical` comparison operator ``__ne__`` would incorrectly evaluate to ``False`` when either element was missing (:issue:`32276`)
296296
- :meth:`Categorical.fillna` now accepts :class:`Categorical` ``other`` argument (:issue:`32420`)
297+
- Bug where :meth:`Categorical.replace` would replace with ``NaN`` whenever the new value and replacement value were equal (:issue:`33288`)
297298

298299
Datetimelike
299300
^^^^^^^^^^^^

pandas/core/arrays/categorical.py

+2
Original file line numberDiff line numberDiff line change
@@ -2447,6 +2447,8 @@ def replace(self, to_replace, value, inplace: bool = False):
24472447
# other cases, like if both to_replace and value are list-like or if
24482448
# to_replace is a dict, are handled separately in NDFrame
24492449
for replace_value, new_value in replace_dict.items():
2450+
if new_value == replace_value:
2451+
continue
24502452
if replace_value in cat.categories:
24512453
if isna(new_value):
24522454
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)