Skip to content

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

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
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
6 changes: 5 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1936,7 +1936,11 @@ def _setitem_with_indexer_frame_value(self, indexer, value: DataFrame, name: str

else:
for loc in ilocs:
item = self.obj.columns[loc]
level_diff = self.obj.columns.nlevels - value.columns.nlevels
Copy link
Member

Choose a reason for hiding this comment

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

This is a change in behavior. I am not sure, if we want this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this is probably not a good idea, I just tried to avoid unexpected NAN when setting values of a multi-indexed Dataframe, from my observation, the unexpected NAN may be caused by item (column index ("x", "a")) in line 1940 is not a column index of value (DataFrame has columns index "a")

Copy link
Member

Choose a reason for hiding this comment

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

The underlying root cause is, that getitem reduces the dimension of the MultiIndex. Not sure how to avoid this when setting.

Copy link
Member

Choose a reason for hiding this comment

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

yes. fixing the underlying issue is probably not an option for the regression fix, i.e. the issue that this PR closes listed in the OP.

A targeted PR for #47649 that could be backported would not address the underlying issue but could maybe make changes around the change made in #47327 to maybe convert the indexer to one that works #47649 (comment).

Copy link
Contributor Author

@xr-chen xr-chen Jul 26, 2022

Choose a reason for hiding this comment

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

pandas/pandas/core/generic.py

Lines 6677 to 6679 in a7c5773

result.loc[:, k] = result[k].fillna(
v, limit=limit, downcast=downcast_k
)

I was making some changes by adding .values at end of line 6679, looks like that works well. Maybe we could ignore the index in fillna since we know result[k] is part of result. @simonjayhawkins

if multiindex_indexer and level_diff > 0 and value.columns.nlevels != 0:
item = self.obj.columns.get_level_values(level_diff)[loc]
else:
item = self.obj.columns[loc]
if item in value:
sub_indexer[1] = item
val = self._align_series(
Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/frame/methods/test_fillna.py
Original file line number Diff line number Diff line change
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
19 changes: 19 additions & 0 deletions pandas/tests/indexing/multiindex/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,25 @@ def test_loc_no_second_level_index(self):
)
tm.assert_frame_equal(res, expected)

def test_loc_setitem_with_multi_index(self):
# GH 47649
result = DataFrame(
{
("x", "a"): np.arange(6),
("x", "b"): np.arange(6),
("y", "c"): np.arange(6),
},
)
expected = DataFrame(
{
("x", "a"): np.arange(6, 0, -1),
("x", "b"): np.arange(6),
("y", "c"): np.arange(6),
},
)
result.loc[:, "x"] = expected["x"]
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"indexer, pos",
Expand Down