Skip to content
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

REF: swap axis before calling Manager.pad_or_backfill #53989

Merged
merged 1 commit into from
Jul 6, 2023
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
2 changes: 1 addition & 1 deletion pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def pad_or_backfill(

meth = missing.clean_fill_method(method)
missing.pad_or_backfill_inplace(
out_data,
out_data.T,
method=meth,
axis=0,
limit=limit,
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6911,7 +6911,7 @@ def _pad_or_backfill(

new_mgr = self._mgr.pad_or_backfill(
method=method,
axis=axis,
axis=self._get_block_manager_axis(axis),
limit=limit,
inplace=inplace,
downcast=downcast,
Expand Down Expand Up @@ -8027,7 +8027,7 @@ def interpolate(

new_data = obj._mgr.pad_or_backfill(
method=method,
axis=axis,
axis=self._get_block_manager_axis(axis),
limit=limit,
limit_area=limit_area,
inplace=inplace,
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1893,8 +1893,8 @@ def pad_or_backfill(
using_cow: bool = False,
) -> list[Block]:
values = self.values
if values.ndim == 2 and axis == 0:
# NDArrayBackedExtensionArray.fillna assumes axis=1
if values.ndim == 2 and axis == 1:
# NDArrayBackedExtensionArray.fillna assumes axis=0
new_values = values.T.fillna(method=method, limit=limit).T
else:
new_values = values.fillna(method=method, limit=limit)
Expand Down
18 changes: 17 additions & 1 deletion pandas/tests/extension/base/dim2.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,27 @@ def test_fillna_2d_method(self, data_missing, method):
assert arr[0].isna().all()
assert not arr[1].isna().any()

result = arr.fillna(method=method)
try:
result = arr.pad_or_backfill(method=method, limit=None)
except AttributeError:
result = arr.fillna(method=method, limit=None)

expected = data_missing.fillna(method=method).repeat(2).reshape(2, 2)
self.assert_extension_array_equal(result, expected)

# Reverse so that backfill is not a no-op.
arr2 = arr[::-1]
assert not arr2[0].isna().any()
assert arr2[1].isna().all()

try:
result2 = arr2.pad_or_backfill(method=method, limit=None)
except AttributeError:
result2 = arr2.fillna(method=method, limit=None)

expected2 = data_missing[::-1].fillna(method=method).repeat(2).reshape(2, 2)
self.assert_extension_array_equal(result2, expected2)

@pytest.mark.parametrize("method", ["mean", "median", "var", "std", "sum", "prod"])
def test_reductions_2d_axis_none(self, data, method):
arr2d = data.reshape(1, -1)
Expand Down