Skip to content

Commit

Permalink
BUG: Categorical.fillna with iterables (#21215)
Browse files Browse the repository at this point in the history
Closes #19788
Closes #21097
  • Loading branch information
TomAugspurger authored and jreback committed May 28, 2018
1 parent 1c2844a commit 36c1f6b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Categorical
^^^^^^^^^^^

- 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`)
- 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`)

Conversion
^^^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pandas.core.dtypes.generic import (
ABCSeries, ABCIndexClass, ABCCategoricalIndex)
from pandas.core.dtypes.missing import isna, notna
from pandas.core.dtypes.inference import is_hashable
from pandas.core.dtypes.cast import (
maybe_infer_to_datetimelike,
coerce_indexer_dtype)
Expand Down Expand Up @@ -1751,7 +1752,7 @@ def fillna(self, value=None, method=None, limit=None):
values[indexer] = values_codes[values_codes != -1]

# If value is not a dict or Series it should be a scalar
elif is_scalar(value):
elif is_hashable(value):
if not isna(value) and value not in self.categories:
raise ValueError("fill value must be in categories")

Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/categorical/test_missing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import collections

import numpy as np
import pytest

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

with tm.assert_raises_regex(ValueError, msg):
cat.fillna(**fillna_kwargs)

@pytest.mark.parametrize("named", [True, False])
def test_fillna_iterable_category(self, named):
# https://github.com/pandas-dev/pandas/issues/21097
if named:
Point = collections.namedtuple("Point", "x y")
else:
Point = lambda *args: args # tuple
cat = Categorical([Point(0, 0), Point(0, 1), None])
result = cat.fillna(Point(0, 0))
expected = Categorical([Point(0, 0), Point(0, 1), Point(0, 0)])

tm.assert_categorical_equal(result, expected)

0 comments on commit 36c1f6b

Please sign in to comment.