Skip to content

EA: fillna should accept same type #32414 #43230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pandas/tests/series/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,28 @@ def test_fillna_categorical_with_new_categories(self, fill_value, expected_outpu
result = ser.fillna(fill_value)
tm.assert_series_equal(result, exp)

@pytest.mark.parametrize(
"fill_value, expected_output",
[
("B", ["A", "B", "B", "B", "C"]),
("C", ["A", "B", "C", "C", "C"])
],
)

def test_series_fill(self, fill_value, expected_output):
# GH#32414
data = ["A", "B", np.nan, np.nan, "C"]
ser = Series(Categorical(data, categories=["A", "B"]))

msg = "Element not present in categories. Cannot be filled in series."
with pytest.raises(TypeError, match=msg):
ser.fillna("D")

exp = Series(Categorical(expected_output, categories=["A", "B"]))
result = ser.fillna(fill_value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not cover the issue. We need to check with a categorical fill_value too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have defined the the fill_value in the pytest parameterized section. There I have provided a fill_value . Do we need to explicitly define that categroical value here??

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please ellaborate on this a little more ?? I find it difficult to understand

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are not testing everything mentioned in the issue. @mroeschke provided a code snippet there which should be covered here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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']

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @phofl, this was the code snippet as being provided by @mroeschke, I have tried to add the same thing. Soo i'm asking should I add "B" instead of that fill_value??
I saw other tests too and they did the same thing, do you want me to explicitly declare "B"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay @phofl , I have checked with categorical_fillna too. I will add the updated code tomorrow positively. I think with that issue will be resolved. Thanks

tm.assert_series_equal(result, exp)


def test_fillna_categorical_raises(self):
data = ["a", np.nan, "b", np.nan, np.nan]
ser = Series(Categorical(data, categories=["a", "b"]))
Expand Down