Skip to content

Commit 92148ff

Browse files
committed
Added new solution to account for limit_area with pad
provided by pandas-dev#34749 Test are green now
1 parent 7c5ad7d commit 92148ff

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

pandas/core/internals/blocks.py

+33-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime, timedelta
2+
import functools
23
import inspect
34
import re
45
from typing import TYPE_CHECKING, Any, List, Optional
@@ -1127,6 +1128,7 @@ def interpolate(
11271128
axis=axis,
11281129
inplace=inplace,
11291130
limit=limit,
1131+
limit_area=limit_area,
11301132
fill_value=fill_value,
11311133
coerce=coerce,
11321134
downcast=downcast,
@@ -1155,6 +1157,7 @@ def _interpolate_with_fill(
11551157
axis: int = 0,
11561158
inplace: bool = False,
11571159
limit: Optional[int] = None,
1160+
limit_area: Optional[str] = None,
11581161
fill_value: Optional[Any] = None,
11591162
coerce: bool = False,
11601163
downcast: Optional[str] = None,
@@ -1176,15 +1179,42 @@ def _interpolate_with_fill(
11761179
# We only get here for non-ExtensionBlock
11771180
fill_value = convert_scalar_for_putitemlike(fill_value, self.values.dtype)
11781181

1179-
values = missing.interpolate_2d(
1180-
values,
1182+
interpolate_2d = functools.partial(
1183+
missing.interpolate_2d,
11811184
method=method,
1182-
axis=axis,
11831185
limit=limit,
11841186
fill_value=fill_value,
11851187
dtype=self.dtype,
11861188
)
11871189

1190+
if limit_area is None:
1191+
values = interpolate_2d(values, axis=axis)
1192+
else:
1193+
def func(values):
1194+
invalid = isna(values)
1195+
1196+
if not invalid.any():
1197+
return values
1198+
1199+
if not invalid.all():
1200+
first = missing.find_valid_index(values, "first")
1201+
last = missing.find_valid_index(values, "last")
1202+
1203+
values = interpolate_2d(values)
1204+
1205+
if limit_area == "inside":
1206+
invalid[first : last + 1] = False
1207+
elif limit_area == "outside":
1208+
invalid[:first] = False
1209+
invalid[last + 1 :] = False
1210+
1211+
values[invalid] = np.nan
1212+
else:
1213+
values = interpolate_2d(values)
1214+
return values
1215+
1216+
values = np.apply_along_axis(func, axis, values)
1217+
11881218
blocks = [self.make_block_same_class(values, ndim=self.ndim)]
11891219
return self._maybe_downcast(blocks, downcast)
11901220

0 commit comments

Comments
 (0)