Skip to content

Commit 90e92fc

Browse files
TomAugspurgerdavid-liu-brattle-1
authored andcommitted
BUG: Categorical.fillna with iterables (pandas-dev#21215)
Closes pandas-dev#19788 Closes pandas-dev#21097
1 parent dfeb975 commit 90e92fc

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v0.23.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Categorical
6666
^^^^^^^^^^^
6767

6868
- Bug in :func:`pandas.util.testing.assert_index_equal` which raised ``AssertionError`` incorrectly, when comparing two :class:`CategoricalIndex` objects with param ``check_categorical=False`` (:issue:`19776`)
69+
- Bug in :meth:`Categorical.fillna` incorrectly raising a ``TypeError`` when `value` the individual categories are iterable and `value` is an iterable (:issue:`21097`, :issue:`19788`)
6970

7071
Conversion
7172
^^^^^^^^^^

pandas/core/arrays/categorical.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pandas.core.dtypes.generic import (
1313
ABCSeries, ABCIndexClass, ABCCategoricalIndex)
1414
from pandas.core.dtypes.missing import isna, notna
15+
from pandas.core.dtypes.inference import is_hashable
1516
from pandas.core.dtypes.cast import (
1617
maybe_infer_to_datetimelike,
1718
coerce_indexer_dtype)
@@ -1751,7 +1752,7 @@ def fillna(self, value=None, method=None, limit=None):
17511752
values[indexer] = values_codes[values_codes != -1]
17521753

17531754
# If value is not a dict or Series it should be a scalar
1754-
elif is_scalar(value):
1755+
elif is_hashable(value):
17551756
if not isna(value) and value not in self.categories:
17561757
raise ValueError("fill value must be in categories")
17571758

pandas/tests/categorical/test_missing.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
import collections
3+
24
import numpy as np
35
import pytest
46

@@ -68,3 +70,16 @@ def test_fillna_raises(self, fillna_kwargs, msg):
6870

6971
with tm.assert_raises_regex(ValueError, msg):
7072
cat.fillna(**fillna_kwargs)
73+
74+
@pytest.mark.parametrize("named", [True, False])
75+
def test_fillna_iterable_category(self, named):
76+
# https://github.com/pandas-dev/pandas/issues/21097
77+
if named:
78+
Point = collections.namedtuple("Point", "x y")
79+
else:
80+
Point = lambda *args: args # tuple
81+
cat = Categorical([Point(0, 0), Point(0, 1), None])
82+
result = cat.fillna(Point(0, 0))
83+
expected = Categorical([Point(0, 0), Point(0, 1), Point(0, 0)])
84+
85+
tm.assert_categorical_equal(result, expected)

0 commit comments

Comments
 (0)