Skip to content
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

BUG: Fix fillna on multi indexed Dataframe doesn't work #47774

Merged
merged 9 commits into from Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Expand Up @@ -917,6 +917,7 @@ Missing
- Bug in :meth:`DataFrame.interpolate` with object-dtype column not returning a copy with ``inplace=False`` (:issue:`45791`)
- Bug in :meth:`DataFrame.dropna` allows to set both ``how`` and ``thresh`` incompatible arguments (:issue:`46575`)
- Bug in :meth:`DataFrame.fillna` ignored ``axis`` when :class:`DataFrame` is single block (:issue:`47713`)
- Bug in :meth:`DataFrame.fillna` not working on multiindexed DataFrame (:issue:`47649`)

MultiIndex
^^^^^^^^^^
Expand Down
9 changes: 7 additions & 2 deletions pandas/core/generic.py
Expand Up @@ -6674,9 +6674,14 @@ def fillna(
if k not in result:
continue
downcast_k = downcast if not is_dict else downcast.get(k)
result.loc[:, k] = result[k].fillna(
v, limit=limit, downcast=downcast_k
# GH47649
result.loc[:, k] = (
result[k].fillna(v, limit=limit, downcast=downcast_k).values
)
# TODO: result.loc[:, k] = result.loc[:, k].fillna(
# v, limit=limit, downcast=downcast_k
# )
# Revert when GH45751 is fixed
return result if not inplace else None

elif not is_list_like(value):
Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/frame/methods/test_fillna.py
Expand Up @@ -708,6 +708,34 @@ def test_single_block_df_with_horizontal_axis(self):
)
tm.assert_frame_equal(result, expected)

def test_fillna_with_multi_index_frame(self):
# GH 47649
pdf = DataFrame(
{
("x", "a"): [np.nan, 2.0, 3.0, 4.0, np.nan, 6.0],
Copy link
Member

Choose a reason for hiding this comment

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

Could you simplify the DataFrame? 2 rows should be sufficient.

Also please add a test with ea dtypes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, could you be more specific about what dtypes we want in the test?

Copy link
Member

Choose a reason for hiding this comment

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

Int64 for example

("x", "b"): [1.0, 2.0, np.nan, 4.0, np.nan, np.nan],
("y", "c"): [1.0, 2.0, 3.0, 4.0, np.nan, np.nan],
}
)
expected = DataFrame(
{
("x", "a"): [-1.0, 2.0, 3.0, 4.0, -1.0, 6.0],
("x", "b"): [1.0, 2.0, -1.0, 4.0, -1.0, -1.0],
("y", "c"): [1.0, 2.0, 3.0, 4.0, np.nan, np.nan],
}
)
tm.assert_frame_equal(pdf.fillna({"x": -1}), expected)
tm.assert_frame_equal(pdf.fillna({"x": -1, ("x", "b"): -2}), expected)

expected = DataFrame(
{
("x", "a"): [-1.0, 2.0, 3.0, 4.0, -1.0, 6.0],
("x", "b"): [1.0, 2.0, -2.0, 4.0, -2.0, -2.0],
("y", "c"): [1.0, 2.0, 3.0, 4.0, np.nan, np.nan],
}
)
tm.assert_frame_equal(pdf.fillna({("x", "b"): -2, "x": -1}), expected)


def test_fillna_nonconsolidated_frame():
# https://github.com/pandas-dev/pandas/issues/36495
Expand Down