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 3 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
20 changes: 12 additions & 8 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7968,11 +7968,8 @@ 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 obj.empty:
if self.empty:
if inplace:
return None
return self.copy()
Expand All @@ -7988,9 +7985,9 @@ def interpolate(
FutureWarning,
stacklevel=find_stack_level(),
)
elif np.any(obj.dtypes == object):
elif np.any(self.dtypes == object):
# GH#53631
if not (obj.ndim == 2 and np.all(obj.dtypes == object)):
if not (self.ndim == 2 and np.all(self.dtypes == object)):
# don't warn in cases that already raise
warnings.warn(
f"{type(self).__name__}.interpolate with object dtype is "
Expand All @@ -8000,14 +7997,14 @@ def interpolate(
stacklevel=find_stack_level(),
)

if isinstance(obj.index, MultiIndex) and method != "linear":
if isinstance(self.index, MultiIndex) and method != "linear":
raise ValueError(
"Only `method=linear` interpolation is supported on MultiIndexes."
)

limit_direction = missing.infer_limit_direction(limit_direction, method)

if obj.ndim == 2 and np.all(obj.dtypes == np.dtype("object")):
if self.ndim == 2 and np.all(self.dtypes == np.dtype("object")):
raise TypeError(
"Cannot interpolate with all object-dtype columns "
"in the DataFrame. Try setting at least one "
Expand All @@ -8024,6 +8021,12 @@ 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
if not self._mgr.is_single_block and axis == 1:
if inplace:
raise NotImplementedError()
obj, axis, should_transpose = self.T, 1 - axis, True
else:
obj, should_transpose = self, False

new_data = obj._mgr.pad_or_backfill(
method=method,
Expand All @@ -8034,6 +8037,7 @@ def interpolate(
downcast=downcast,
)
else:
obj, should_transpose = (self.T, True) if axis == 1 else (self, False)
index = missing.get_interp_index(method, obj.index)
axis = self._info_axis_number
new_data = obj._mgr.interpolate(
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