Skip to content

REF: implement Manager.pad_or_backfill #53822

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 1 commit into from
Jun 25, 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
42 changes: 27 additions & 15 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6876,7 +6876,7 @@ def _fillna_with_method(

return result

new_mgr = self._mgr.interpolate(
new_mgr = self._mgr.pad_or_backfill(
method=method,
axis=axis,
limit=limit,
Expand Down Expand Up @@ -7957,9 +7957,6 @@ def interpolate(
stacklevel=find_stack_level(),
)

if method not in fillna_methods:
axis = self._info_axis_number

if isinstance(obj.index, MultiIndex) and method != "linear":
raise ValueError(
"Only `method=linear` interpolation is supported on MultiIndexes."
Expand All @@ -7976,17 +7973,32 @@ def interpolate(

index = missing.get_interp_index(method, obj.index)

new_data = obj._mgr.interpolate(
method=method,
axis=axis,
index=index,
limit=limit,
limit_direction=limit_direction,
limit_area=limit_area,
inplace=inplace,
downcast=downcast,
**kwargs,
)
if method.lower() in fillna_methods:
# TODO(3.0): remove this case
new_data = obj._mgr.pad_or_backfill(
method=method,
axis=axis,
index=index,
limit=limit,
limit_direction=limit_direction,
limit_area=limit_area,
inplace=inplace,
downcast=downcast,
**kwargs,
)
else:
axis = self._info_axis_number
new_data = obj._mgr.interpolate(
method=method,
axis=axis,
index=index,
limit=limit,
limit_direction=limit_direction,
limit_area=limit_area,
inplace=inplace,
downcast=downcast,
**kwargs,
)

result = self._constructor(new_data)
if should_transpose:
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ def diff(self, n: int, axis: AxisInt) -> Self:
assert self.ndim == 2 and axis == 0 # caller ensures
return self.apply(algos.diff, n=n, axis=axis)

def pad_or_backfill(self, **kwargs) -> Self:
return self.apply_with_block("pad_or_backfill", swap_axis=False, **kwargs)

def interpolate(self, **kwargs) -> Self:
return self.apply_with_block("interpolate", swap_axis=False, **kwargs)

Expand Down
29 changes: 29 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,35 @@ def fillna(
]
)

def pad_or_backfill(
self,
*,
method: FillnaOptions = "pad",
axis: AxisInt = 0,
index: Index | None = None,
inplace: bool = False,
limit: int | None = None,
limit_direction: Literal["forward", "backward", "both"] = "forward",
limit_area: str | None = None,
fill_value: Any | None = None,
downcast: Literal["infer"] | None = None,
using_cow: bool = False,
**kwargs,
) -> list[Block]:
return self.interpolate(
method=method,
axis=axis,
index=index,
inplace=inplace,
limit=limit,
limit_direction=limit_direction,
limit_area=limit_area,
fill_value=fill_value,
downcast=downcast,
using_cow=using_cow,
**kwargs,
)

def interpolate(
self,
*,
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,14 @@ def diff(self, n: int, axis: AxisInt) -> Self:
axis = self._normalize_axis(axis)
return self.apply("diff", n=n, axis=axis)

def pad_or_backfill(self, inplace: bool, **kwargs) -> Self:
return self.apply(
"pad_or_backfill",
inplace=inplace,
**kwargs,
using_cow=using_copy_on_write(),
)

def interpolate(self, inplace: bool, **kwargs) -> Self:
return self.apply(
"interpolate", inplace=inplace, **kwargs, using_cow=using_copy_on_write()
Expand Down