Skip to content

Commit 1bf6c88

Browse files
jbrockmendelJulianWgs
authored andcommitted
BUG: Categorical.fillna with non-category tuple (pandas-dev#41914)
1 parent f1ddf64 commit 1bf6c88

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,7 @@ Categorical
875875
- Bug in constructing a :class:`DataFrame` from an ``ndarray`` and a :class:`CategoricalDtype` (:issue:`38857`)
876876
- Bug in setting categorical values into an object-dtype column in a :class:`DataFrame` (:issue:`39136`)
877877
- Bug in :meth:`DataFrame.reindex` was raising an ``IndexError`` when the new index contained duplicates and the old index was a :class:`CategoricalIndex` (:issue:`38906`)
878+
- Bug in :meth:`Categorical.fillna` with a tuple-like category raising ``NotImplementedError`` instead of ``ValueError`` when filling with a non-category tuple (:issue:`41914`)
878879

879880
Datetimelike
880881
^^^^^^^^^^^^

pandas/core/arrays/categorical.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2030,7 +2030,8 @@ def _validate_setitem_value(self, value):
20302030

20312031
from pandas import Index
20322032

2033-
to_add = Index(rvalue).difference(self.categories)
2033+
# tupleize_cols=False for e.g. test_fillna_iterable_category GH#41914
2034+
to_add = Index(rvalue, tupleize_cols=False).difference(self.categories)
20342035

20352036
# no assignments of values not in categories, but it's always ok to set
20362037
# something to np.nan

pandas/tests/arrays/categorical/test_missing.py

+7
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ def test_fillna_iterable_category(self, named):
100100

101101
tm.assert_categorical_equal(result, expected)
102102

103+
# Case where the Point is not among our categories; we want ValueError,
104+
# not NotImplementedError GH#41914
105+
cat = Categorical(np.array([Point(1, 0), Point(0, 1), None], dtype=object))
106+
msg = "Cannot setitem on a Categorical with a new category"
107+
with pytest.raises(ValueError, match=msg):
108+
cat.fillna(Point(0, 0))
109+
103110
def test_fillna_array(self):
104111
# accept Categorical or ndarray value if it holds appropriate values
105112
cat = Categorical(["A", "B", "C", None, None])

0 commit comments

Comments
 (0)