Skip to content

BUG: df.fillna ignores axis when df is single block #47714

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 7 commits into from
Jul 14, 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.
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 @@ -914,6 +914,7 @@ Missing
- Bug in :meth:`Series.fillna` and :meth:`DataFrame.fillna` with :class:`IntervalDtype` and incompatible value raising instead of casting to a common (usually object) dtype (:issue:`45796`)
- 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`)

MultiIndex
^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6679,7 +6679,7 @@ def fillna(
return result if not inplace else None

elif not is_list_like(value):
if not self._mgr.is_single_block and axis == 1:
Copy link
Member

Choose a reason for hiding this comment

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

@jbrockmendel should this be fixed in the block manager fillna?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is also my question. Originally, I thought delete single block check would fail some tests but it was not. Thus get confused why we need this.

Copy link
Member

Choose a reason for hiding this comment

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

should this be fixed in the block manager fillna?

i dont think so, the BlockManager method doesn't have an axis keyword

if axis == 1:

result = self.T.fillna(value=value, limit=limit).T

Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/frame/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,29 @@ def test_inplace_dict_update_view(self, val):
tm.assert_frame_equal(df, expected)
tm.assert_frame_equal(result_view, expected)

def test_single_block_df_with_horizontal_axis(self):
# GH 47713
df = DataFrame(
{
"col1": [5, 0, np.nan, 10, np.nan],
"col2": [7, np.nan, np.nan, 5, 3],
"col3": [12, np.nan, 1, 2, 0],
"col4": [np.nan, 1, 1, np.nan, 18],
}
)
result = df.fillna(50, limit=1, axis=1)
expected = DataFrame(
[
[5.0, 7.0, 12.0, 50.0],
[0.0, 50.0, np.nan, 1.0],
[50.0, np.nan, 1.0, 1.0],
[10.0, 5.0, 2.0, 50.0],
[50.0, 3.0, 0.0, 18.0],
],
columns=["col1", "col2", "col3", "col4"],
)
tm.assert_frame_equal(result, expected)


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