Skip to content

Commit 90adfff

Browse files
jbrockmendelim-vinicius
authored and
im-vinicius
committed
REF: implement Manager.pad_or_backfill (pandas-dev#53822)
1 parent af27d92 commit 90adfff

File tree

4 files changed

+67
-15
lines changed

4 files changed

+67
-15
lines changed

pandas/core/generic.py

+27-15
Original file line numberDiff line numberDiff line change
@@ -6912,7 +6912,7 @@ def _fillna_with_method(
69126912

69136913
return result
69146914

6915-
new_mgr = self._mgr.interpolate(
6915+
new_mgr = self._mgr.pad_or_backfill(
69166916
method=method,
69176917
axis=axis,
69186918
limit=limit,
@@ -7989,9 +7989,6 @@ def interpolate(
79897989
stacklevel=find_stack_level(),
79907990
)
79917991

7992-
if method not in fillna_methods:
7993-
axis = self._info_axis_number
7994-
79957992
if isinstance(obj.index, MultiIndex) and method != "linear":
79967993
raise ValueError(
79977994
"Only `method=linear` interpolation is supported on MultiIndexes."
@@ -8008,17 +8005,32 @@ def interpolate(
80088005

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

8011-
new_data = obj._mgr.interpolate(
8012-
method=method,
8013-
axis=axis,
8014-
index=index,
8015-
limit=limit,
8016-
limit_direction=limit_direction,
8017-
limit_area=limit_area,
8018-
inplace=inplace,
8019-
downcast=downcast,
8020-
**kwargs,
8021-
)
8008+
if method.lower() in fillna_methods:
8009+
# TODO(3.0): remove this case
8010+
new_data = obj._mgr.pad_or_backfill(
8011+
method=method,
8012+
axis=axis,
8013+
index=index,
8014+
limit=limit,
8015+
limit_direction=limit_direction,
8016+
limit_area=limit_area,
8017+
inplace=inplace,
8018+
downcast=downcast,
8019+
**kwargs,
8020+
)
8021+
else:
8022+
axis = self._info_axis_number
8023+
new_data = obj._mgr.interpolate(
8024+
method=method,
8025+
axis=axis,
8026+
index=index,
8027+
limit=limit,
8028+
limit_direction=limit_direction,
8029+
limit_area=limit_area,
8030+
inplace=inplace,
8031+
downcast=downcast,
8032+
**kwargs,
8033+
)
80228034

80238035
result = self._constructor_from_mgr(new_data, axes=new_data.axes)
80248036
if should_transpose:

pandas/core/internals/array_manager.py

+3
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ def diff(self, n: int) -> Self:
357357
assert self.ndim == 2 # caller ensures
358358
return self.apply(algos.diff, n=n)
359359

360+
def pad_or_backfill(self, **kwargs) -> Self:
361+
return self.apply_with_block("pad_or_backfill", swap_axis=False, **kwargs)
362+
360363
def interpolate(self, **kwargs) -> Self:
361364
return self.apply_with_block("interpolate", swap_axis=False, **kwargs)
362365

pandas/core/internals/blocks.py

+29
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,35 @@ def fillna(
13431343
]
13441344
)
13451345

1346+
def pad_or_backfill(
1347+
self,
1348+
*,
1349+
method: FillnaOptions = "pad",
1350+
axis: AxisInt = 0,
1351+
index: Index | None = None,
1352+
inplace: bool = False,
1353+
limit: int | None = None,
1354+
limit_direction: Literal["forward", "backward", "both"] = "forward",
1355+
limit_area: str | None = None,
1356+
fill_value: Any | None = None,
1357+
downcast: Literal["infer"] | None = None,
1358+
using_cow: bool = False,
1359+
**kwargs,
1360+
) -> list[Block]:
1361+
return self.interpolate(
1362+
method=method,
1363+
axis=axis,
1364+
index=index,
1365+
inplace=inplace,
1366+
limit=limit,
1367+
limit_direction=limit_direction,
1368+
limit_area=limit_area,
1369+
fill_value=fill_value,
1370+
downcast=downcast,
1371+
using_cow=using_cow,
1372+
**kwargs,
1373+
)
1374+
13461375
def interpolate(
13471376
self,
13481377
*,

pandas/core/internals/managers.py

+8
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,14 @@ def diff(self, n: int) -> Self:
415415
# only reached with self.ndim == 2
416416
return self.apply("diff", n=n)
417417

418+
def pad_or_backfill(self, inplace: bool, **kwargs) -> Self:
419+
return self.apply(
420+
"pad_or_backfill",
421+
inplace=inplace,
422+
**kwargs,
423+
using_cow=using_copy_on_write(),
424+
)
425+
418426
def interpolate(self, inplace: bool, **kwargs) -> Self:
419427
return self.apply(
420428
"interpolate", inplace=inplace, **kwargs, using_cow=using_copy_on_write()

0 commit comments

Comments
 (0)