Skip to content

Commit 6ed5f08

Browse files
author
Marco Gorelli
committed
Ensure that :func: in :class: only replaces null values
1 parent 6813d77 commit 6ed5f08

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

doc/source/whatsnew/v0.25.1.rst

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Other enhancements
2121
Bug fixes
2222
~~~~~~~~~
2323

24+
- Bug in :func:`fill_na` in :class:`Categorical` would replace all values, not just those that are NaN (:issue:`26215`)
25+
2426

2527
Categorical
2628
^^^^^^^^^^^

pandas/core/arrays/categorical.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1824,7 +1824,6 @@ def fillna(self, value=None, method=None, limit=None):
18241824

18251825
# pad / bfill
18261826
if method is not None:
1827-
18281827
values = self.to_dense().reshape(-1, len(self))
18291828
values = interpolate_2d(values, method, 0, None, value).astype(
18301829
self.categories.dtype
@@ -1838,10 +1837,9 @@ def fillna(self, value=None, method=None, limit=None):
18381837
if isinstance(value, ABCSeries):
18391838
if not value[~value.isin(self.categories)].isna().all():
18401839
raise ValueError("fill value must be in categories")
1841-
18421840
values_codes = _get_codes_for_values(value, self.categories)
1843-
indexer = np.where(values_codes != -1)
1844-
codes[indexer] = values_codes[values_codes != -1]
1841+
indexer = np.where(codes == -1)
1842+
codes[indexer] = values_codes[codes == -1]
18451843

18461844
# If value is not a dict or Series it should be a scalar
18471845
elif is_hashable(value):

pandas/tests/series/test_missing.py

+22
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,28 @@ def test_fillna_categorical(self, fill_value, expected_output):
578578
exp = Series(Categorical(expected_output, categories=["a", "b"]))
579579
tm.assert_series_equal(s.fillna(fill_value), exp)
580580

581+
@pytest.mark.parametrize(
582+
"new_categories, fill_value, expected_output",
583+
[
584+
(
585+
["c", "d", "e"],
586+
Series(["a", "b", "c", "d", "e"]),
587+
["a", "b", "b", "d", "e"],
588+
)
589+
],
590+
)
591+
def test_fillna_categorical_with_new_categories(
592+
self, new_categories, fill_value, expected_output
593+
):
594+
# GH 26215
595+
data = ["a", np.nan, "b", np.nan, np.nan]
596+
s = Series(Categorical(data, categories=["a", "b"]))
597+
s.cat.add_categories(new_categories, inplace=True)
598+
exp = Series(
599+
Categorical(expected_output, categories=["a", "b"] + new_categories)
600+
)
601+
tm.assert_series_equal(s.fillna(fill_value), exp)
602+
581603
def test_fillna_categorical_raise(self):
582604
data = ["a", np.nan, "b", np.nan, np.nan]
583605
s = Series(Categorical(data, categories=["a", "b"]))

0 commit comments

Comments
 (0)