From 24cb2607148a9e4f6be99433a6b41746e4ca8bc2 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 9 Jun 2021 16:02:07 -0700 Subject: [PATCH 1/2] BUG: Categorical.fillna with non-category tuple --- doc/source/whatsnew/v1.3.0.rst | 1 + pandas/core/arrays/categorical.py | 3 ++- pandas/tests/arrays/categorical/test_missing.py | 7 +++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index ff1c6ebf7aae2..6cecd5818cbea 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -874,6 +874,7 @@ Categorical - Bug in constructing a :class:`DataFrame` from an ``ndarray`` and a :class:`CategoricalDtype` (:issue:`38857`) - Bug in setting categorical values into an object-dtype column in a :class:`DataFrame` (:issue:`39136`) - 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`) +- Bug in :meth:`Categorical.fillna` with a tuple-like category raising ``NotImplementedError`` instead of ``ValueError`` when filling with a non-category tuple (:issue:`??`) Datetimelike ^^^^^^^^^^^^ diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 5fdfaf9ff0c7f..510ce5f9d48d7 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -2030,7 +2030,8 @@ def _validate_setitem_value(self, value): from pandas import Index - to_add = Index(rvalue).difference(self.categories) + # tupleize_cols=False for e.g. test_fillna_iterable_category + to_add = Index(rvalue, tupleize_cols=False).difference(self.categories) # no assignments of values not in categories, but it's always ok to set # something to np.nan diff --git a/pandas/tests/arrays/categorical/test_missing.py b/pandas/tests/arrays/categorical/test_missing.py index c765416368726..4d44d8a116840 100644 --- a/pandas/tests/arrays/categorical/test_missing.py +++ b/pandas/tests/arrays/categorical/test_missing.py @@ -100,6 +100,13 @@ def test_fillna_iterable_category(self, named): tm.assert_categorical_equal(result, expected) + # Case where the Point is not among our categories; we want ValueError, + # not NotImplementedError + cat = Categorical(np.array([Point(1, 0), Point(0, 1), None], dtype=object)) + msg = "Cannot setitem on a Categorical with a new category" + with pytest.raises(ValueError, match=msg): + cat.fillna(Point(0, 0)) + def test_fillna_array(self): # accept Categorical or ndarray value if it holds appropriate values cat = Categorical(["A", "B", "C", None, None]) From 667343added264827f75d4ef5beed93966c6a5e3 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 9 Jun 2021 16:03:16 -0700 Subject: [PATCH 2/2] GH refs --- doc/source/whatsnew/v1.3.0.rst | 2 +- pandas/core/arrays/categorical.py | 2 +- pandas/tests/arrays/categorical/test_missing.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 6cecd5818cbea..557cefc2ed07f 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -874,7 +874,7 @@ Categorical - Bug in constructing a :class:`DataFrame` from an ``ndarray`` and a :class:`CategoricalDtype` (:issue:`38857`) - Bug in setting categorical values into an object-dtype column in a :class:`DataFrame` (:issue:`39136`) - 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`) -- Bug in :meth:`Categorical.fillna` with a tuple-like category raising ``NotImplementedError`` instead of ``ValueError`` when filling with a non-category tuple (:issue:`??`) +- Bug in :meth:`Categorical.fillna` with a tuple-like category raising ``NotImplementedError`` instead of ``ValueError`` when filling with a non-category tuple (:issue:`41914`) Datetimelike ^^^^^^^^^^^^ diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 510ce5f9d48d7..9241a1b7632b9 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -2030,7 +2030,7 @@ def _validate_setitem_value(self, value): from pandas import Index - # tupleize_cols=False for e.g. test_fillna_iterable_category + # tupleize_cols=False for e.g. test_fillna_iterable_category GH#41914 to_add = Index(rvalue, tupleize_cols=False).difference(self.categories) # no assignments of values not in categories, but it's always ok to set diff --git a/pandas/tests/arrays/categorical/test_missing.py b/pandas/tests/arrays/categorical/test_missing.py index 4d44d8a116840..930d890ee91d4 100644 --- a/pandas/tests/arrays/categorical/test_missing.py +++ b/pandas/tests/arrays/categorical/test_missing.py @@ -101,7 +101,7 @@ def test_fillna_iterable_category(self, named): tm.assert_categorical_equal(result, expected) # Case where the Point is not among our categories; we want ValueError, - # not NotImplementedError + # not NotImplementedError GH#41914 cat = Categorical(np.array([Point(1, 0), Point(0, 1), None], dtype=object)) msg = "Cannot setitem on a Categorical with a new category" with pytest.raises(ValueError, match=msg):