Skip to content

BUG: interpolate with fillna methods fail to fill across multiblocks #53962

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 13 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ Indexing

Missing
^^^^^^^
- Bug in :meth:`DataFrame.interpolate` failing to fill across multiblock data when ``method`` is "pad", "ffill", "bfill", or "backfill" (:issue:`53898`)
- Bug in :meth:`DataFrame.interpolate` ignoring ``inplace`` when :class:`DataFrame` is empty (:issue:`53199`)
- Bug in :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` failing to raise on invalid ``downcast`` keyword, which can be only ``None`` or "infer" (:issue:`53103`)
- Bug in :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` with complex dtype incorrectly failing to fill ``NaN`` entries (:issue:`53635`)
Expand Down
13 changes: 10 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7968,9 +7968,17 @@ def interpolate(
axis = self._get_axis_number(axis)

fillna_methods = ["ffill", "bfill", "pad", "backfill"]
should_transpose = axis == 1 and method not in fillna_methods

obj = self.T if should_transpose else self
if method not in fillna_methods:
should_transpose = axis == 1
elif not self._mgr.is_single_block and axis == 1:
if inplace:
raise NotImplementedError()
should_transpose = True
else:
should_transpose = False

obj, axis = (self.T, 1 - axis) if should_transpose else (self, axis)

if obj.empty:
if inplace:
Expand Down Expand Up @@ -8024,7 +8032,6 @@ def interpolate(
# TODO(3.0): remove this case
# TODO: warn/raise on limit_direction or kwargs which are ignored?
# as of 2023-06-26 no tests get here with either

new_data = obj._mgr.pad_or_backfill(
method=method,
axis=axis,
Expand Down
9 changes: 8 additions & 1 deletion pandas/tests/frame/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,11 @@ def test_interp_string_axis(self, axis_name, axis_number):
expected = df.interpolate(method="linear", axis=axis_number)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("multiblock", [True, False])
@pytest.mark.parametrize("method", ["ffill", "bfill", "pad"])
def test_interp_fillna_methods(self, request, axis, method, using_array_manager):
def test_interp_fillna_methods(
self, request, axis, multiblock, method, using_array_manager
):
# GH 12918
if using_array_manager and axis in (1, "columns"):
# TODO(ArrayManager) support axis=1
Expand All @@ -465,6 +468,10 @@ def test_interp_fillna_methods(self, request, axis, method, using_array_manager)
"C": [3.0, 6.0, 9.0, np.nan, np.nan, 30.0],
}
)
if multiblock:
df["D"] = np.nan
df["E"] = 1.0

method2 = method if method != "pad" else "ffill"
expected = getattr(df, method2)(axis=axis)
msg = f"DataFrame.interpolate with method={method} is deprecated"
Expand Down