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 all 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.4.4.rst
Expand Up @@ -14,6 +14,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed regression in :meth:`DataFrame.fillna` not working :class:`DataFrame` with :class:`MultiIndex` (:issue:`47649`)
- Fixed regression in taking NULL :class:`objects` from a :class:`DataFrame` causing a segmentation violation. These NULL values are created by :meth:`numpy.empty_like` (:issue:`46848`)
- Fixed regression in :func:`concat` materializing :class:`Index` during sorting even if :class:`Index` was already sorted (:issue:`47501`)
- Fixed regression in calling bitwise numpy ufuncs (for example, ``np.bitwise_and``) on Index objects (:issue:`46769`)
Expand Down
10 changes: 8 additions & 2 deletions pandas/core/generic.py
Expand Up @@ -6861,16 +6861,22 @@ def fillna(
for k, v in value.items():
if k not in result:
continue

# error: Item "None" of "Optional[Dict[Any, Any]]" has no
# attribute "get"
downcast_k = (
downcast
if not is_dict
else downcast.get(k) # type: ignore[union-attr]
)
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 @@ -715,6 +715,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],
("x", "b"): [1.0, 2.0, np.nan],
("y", "c"): [1.0, 2.0, np.nan],
}
)
expected = DataFrame(
{
("x", "a"): [-1.0, 2.0, 3.0],
("x", "b"): [1.0, 2.0, -1.0],
("y", "c"): [1.0, 2.0, 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],
("x", "b"): [1.0, 2.0, -2.0],
("y", "c"): [1.0, 2.0, 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