Skip to content

Commit c92c953

Browse files
Backport PR #47774 on branch 1.4.x (BUG: Fix fillna on multi indexed Dataframe doesn't work) (#48216)
Backport PR #47774: BUG: Fix fillna on multi indexed Dataframe doesn't work Co-authored-by: Xingrong Chen <[email protected]>
1 parent 2f71a9c commit c92c953

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

doc/source/whatsnew/v1.4.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ including other versions of pandas.
1414

1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
17+
- Fixed regression in :meth:`DataFrame.fillna` not working :class:`DataFrame` with :class:`MultiIndex` (:issue:`47649`)
1718
- 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`)
1819
- Fixed regression in :func:`concat` materializing :class:`Index` during sorting even if :class:`Index` was already sorted (:issue:`47501`)
1920
- Fixed regression in calling bitwise numpy ufuncs (for example, ``np.bitwise_and``) on Index objects (:issue:`46769`)

pandas/core/generic.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -6482,9 +6482,14 @@ def fillna(
64826482
if k not in result:
64836483
continue
64846484
downcast_k = downcast if not is_dict else downcast.get(k)
6485-
result.loc[:, k] = result[k].fillna(
6486-
v, limit=limit, downcast=downcast_k
6485+
# GH47649
6486+
result.loc[:, k] = (
6487+
result[k].fillna(v, limit=limit, downcast=downcast_k).values
64876488
)
6489+
# TODO: result.loc[:, k] = result.loc[:, k].fillna(
6490+
# v, limit=limit, downcast=downcast_k
6491+
# )
6492+
# Revert when GH45751 is fixed
64886493
return result if not inplace else None
64896494

64906495
elif not is_list_like(value):

pandas/tests/frame/methods/test_fillna.py

+28
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,34 @@ def test_inplace_dict_update_view(self, val):
666666
tm.assert_frame_equal(df, expected)
667667
tm.assert_frame_equal(result_view, expected)
668668

669+
def test_fillna_with_multi_index_frame(self):
670+
# GH 47649
671+
pdf = DataFrame(
672+
{
673+
("x", "a"): [np.nan, 2.0, 3.0],
674+
("x", "b"): [1.0, 2.0, np.nan],
675+
("y", "c"): [1.0, 2.0, np.nan],
676+
}
677+
)
678+
expected = DataFrame(
679+
{
680+
("x", "a"): [-1.0, 2.0, 3.0],
681+
("x", "b"): [1.0, 2.0, -1.0],
682+
("y", "c"): [1.0, 2.0, np.nan],
683+
}
684+
)
685+
tm.assert_frame_equal(pdf.fillna({"x": -1}), expected)
686+
tm.assert_frame_equal(pdf.fillna({"x": -1, ("x", "b"): -2}), expected)
687+
688+
expected = DataFrame(
689+
{
690+
("x", "a"): [-1.0, 2.0, 3.0],
691+
("x", "b"): [1.0, 2.0, -2.0],
692+
("y", "c"): [1.0, 2.0, np.nan],
693+
}
694+
)
695+
tm.assert_frame_equal(pdf.fillna({("x", "b"): -2, "x": -1}), expected)
696+
669697

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

0 commit comments

Comments
 (0)