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 all 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 @@ -7,6 +7,7 @@
import numpy as np
import pytest
import pytz
from typing_extensions import Literal

from pandas import (
Categorical,
Expand Down Expand Up @@ -671,6 +672,27 @@ 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_fillna_categorical(self, fill_value, expected_output):
# GH32414
data = ["A", "B", np.nan, np.nan, "C"]
cat = Categorical(data, categories=["A", "B", "C"])
ser = Series(cat)
exp_cat = Categorical(expected_output, categories=["A", "B", "C"])
exp_ser = Series(exp_cat)
result_ser = ser.fillna(fill_value)
filled = cat.fillna(fill_value)
tm.assert_almost_equal(result_ser, exp_ser)
tm.assert_almost_equal(filled, exp_cat)


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