Repository: pandas-dev/pandas
Pull request: Update test_fillna.py
- [x] closes #32414 
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.


Problem statement:
EA: fillna should accept same type
```
cat = pd.Categorical(["A", "B", None, "A"])
ser = pd.Series(cat).fillna("B")

>>> filled = cat.fillna(ser)  # <-- works

>>> cat.fillna(filled)
TypeError: 'value' parameter must be a scalar, dict or Series, but you passed a Categorical
```

Hints from issue discussion:
@jbrockmendel Is this issue appropriate for first-time contributors?
> Is this issue appropriate for first-time contributors?

Probably not; it gets pretty deep into the weeds.  #32402 might be a good fit if you're comfortable with cython, otherwise i suggest searching for the Good First Issue label
I think this work on master. Could use a test

```
In [22]: cat = pd.Categorical(["A", "B", None, "A"])
    ...: ser = pd.Series(cat).fillna("B")

In [23]: >>> filled = cat.fillna(ser)

In [24]: >>> cat.fillna(filled)
Out[24]:
['A', 'B', 'B', 'A']
Categories (2, object): ['A', 'B']
```
hey @mroeschke , Could you please tell on which cases should we really test it on?

@Bhavay192 This entire code snippet should be tested
Hey @mroeschke , thanks for replying. Soo according to you I should take different values of numpy arrays and test it on that??
The exact code snippet above should be written as a unit test and added to the testing suite: https://pandas.pydata.org/pandas-docs/stable/development/contributing_codebase.html#writing-tests
Hey @mroeschke, Since I'm quite new to open source and unit testing. Could you please help with in finding out what all test should I Perform on it??
I can take test of input values and what should be the expected output. Any other test that I can perform??
Hey @mroeschke, I have written a small unit test for the above script. Now how will you review it please tell. Shall I send a PR?? Also please tell in which package shall I add my test into??  
You can follow this guide to submit a PR and maintainers can help provide further guidance: https://pandas.pydata.org/pandas-docs/stable/development/contributing.html#contributing-your-changes-to-pandas

Commit message: change test function name

Patch to review:
diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py
index 71a1b01eb9157..f8ee6aa253e18 100644
--- a/pandas/tests/series/methods/test_fillna.py
+++ b/pandas/tests/series/methods/test_fillna.py
@@ -851,6 +851,24 @@ def test_fillna_pos_args_deprecation(self):
         expected = Series([1, 2, 3, 0], dtype=float)
         tm.assert_series_equal(result, expected)
 
+    @pytest.mark.parametrize(
+        "input, input_fillna, expected_data, expected_categories",
+        [
+            (["A", "B", None, "A"], "B", ["A", "B", "B", "A"], ["A", "B"]),
+            (["A", "B", np.nan, "A"], "B", ["A", "B", "B", "A"], ["A", "B"]),
+        ],
+    )
+    def test_fillna_categorical_accept_same_type(
+        self, input, input_fillna, expected_data, expected_categories
+    ):
+        # GH32414
+        cat = Categorical(input)
+        ser = Series(cat).fillna(input_fillna)
+        filled = cat.fillna(ser)
+        result = cat.fillna(filled)
+        expected = Categorical(expected_data, categories=expected_categories)
+        tm.assert_almost_equal(result, expected)
+
 
 class TestFillnaPad:
     def test_fillna_bug(self):


Code review comments:
diff_hunk: @@ -851,6 +851,24 @@ def test_fillna_pos_args_deprecation(self):
         expected = Series([1, 2, 3, 0], dtype=float)
         tm.assert_series_equal(result, expected)
 
+    @pytest.mark.parametrize(
+        "input, input_fillna, expected_data, expected_categories",
+        [
+            (["A", "B", None, "A"], "B", ["A", "B", "B", "A"], ["A", "B"]),
+            (["A", "B", np.nan, "A"], "B", ["A", "B", "B", "A"], ["A", "B"]),
+        ],
+    )
+    def test_fillna_categorical_accept_same_type(
+        self, input, input_fillna, expected_data, expected_categories
+    ):
+        # GH32414
+        cat = Categorical(input)
+        ser = Series(cat).fillna(input_fillna)
+        filled = cat.fillna(ser)
+        result = cat.fillna(filled)
+        expected = Categorical(expected_data, categories=expected_categories)
+        tm.assert_almost_equal(result, expected)
path: pandas/tests/series/methods/test_fillna.py
text: @user1:
If you're testing Categoricals please use `tm.assert_categorical_equal`