diff --git a/doc/source/whatsnew/v1.4.4.rst b/doc/source/whatsnew/v1.4.4.rst index dbdeebad06af0..3d0a6e01826f8 100644 --- a/doc/source/whatsnew/v1.4.4.rst +++ b/doc/source/whatsnew/v1.4.4.rst @@ -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`) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 8b1a427e1658a..abab32ae145bd 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6861,6 +6861,7 @@ 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 = ( @@ -6868,9 +6869,14 @@ def fillna( 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): diff --git a/pandas/tests/frame/methods/test_fillna.py b/pandas/tests/frame/methods/test_fillna.py index 20e59ed72666a..8355502c47c61 100644 --- a/pandas/tests/frame/methods/test_fillna.py +++ b/pandas/tests/frame/methods/test_fillna.py @@ -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