Skip to content

Commit ae291e9

Browse files
authored
BUG: Fix fillna on multi indexed Dataframe doesn't work (#47774)
1 parent eb226bd commit ae291e9

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-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

+8-2
Original file line numberDiff line numberDiff line change
@@ -6861,16 +6861,22 @@ def fillna(
68616861
for k, v in value.items():
68626862
if k not in result:
68636863
continue
6864+
68646865
# error: Item "None" of "Optional[Dict[Any, Any]]" has no
68656866
# attribute "get"
68666867
downcast_k = (
68676868
downcast
68686869
if not is_dict
68696870
else downcast.get(k) # type: ignore[union-attr]
68706871
)
6871-
result.loc[:, k] = result[k].fillna(
6872-
v, limit=limit, downcast=downcast_k
6872+
# GH47649
6873+
result.loc[:, k] = (
6874+
result[k].fillna(v, limit=limit, downcast=downcast_k).values
68736875
)
6876+
# TODO: result.loc[:, k] = result.loc[:, k].fillna(
6877+
# v, limit=limit, downcast=downcast_k
6878+
# )
6879+
# Revert when GH45751 is fixed
68746880
return result if not inplace else None
68756881

68766882
elif not is_list_like(value):

pandas/tests/frame/methods/test_fillna.py

+28
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,34 @@ def test_single_block_df_with_horizontal_axis(self):
715715
)
716716
tm.assert_frame_equal(result, expected)
717717

718+
def test_fillna_with_multi_index_frame(self):
719+
# GH 47649
720+
pdf = DataFrame(
721+
{
722+
("x", "a"): [np.nan, 2.0, 3.0],
723+
("x", "b"): [1.0, 2.0, np.nan],
724+
("y", "c"): [1.0, 2.0, np.nan],
725+
}
726+
)
727+
expected = DataFrame(
728+
{
729+
("x", "a"): [-1.0, 2.0, 3.0],
730+
("x", "b"): [1.0, 2.0, -1.0],
731+
("y", "c"): [1.0, 2.0, np.nan],
732+
}
733+
)
734+
tm.assert_frame_equal(pdf.fillna({"x": -1}), expected)
735+
tm.assert_frame_equal(pdf.fillna({"x": -1, ("x", "b"): -2}), expected)
736+
737+
expected = DataFrame(
738+
{
739+
("x", "a"): [-1.0, 2.0, 3.0],
740+
("x", "b"): [1.0, 2.0, -2.0],
741+
("y", "c"): [1.0, 2.0, np.nan],
742+
}
743+
)
744+
tm.assert_frame_equal(pdf.fillna({("x", "b"): -2, "x": -1}), expected)
745+
718746

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

0 commit comments

Comments
 (0)