Skip to content

Commit

Permalink
Added new solution to account for limit_area with pad
Browse files Browse the repository at this point in the history
provided by pandas-dev#34749

Test are green now
  • Loading branch information
cchwala committed Aug 26, 2020
1 parent 7c5ad7d commit 92148ff
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime, timedelta
import functools
import inspect
import re
from typing import TYPE_CHECKING, Any, List, Optional
Expand Down Expand Up @@ -1127,6 +1128,7 @@ def interpolate(
axis=axis,
inplace=inplace,
limit=limit,
limit_area=limit_area,
fill_value=fill_value,
coerce=coerce,
downcast=downcast,
Expand Down Expand Up @@ -1155,6 +1157,7 @@ def _interpolate_with_fill(
axis: int = 0,
inplace: bool = False,
limit: Optional[int] = None,
limit_area: Optional[str] = None,
fill_value: Optional[Any] = None,
coerce: bool = False,
downcast: Optional[str] = None,
Expand All @@ -1176,15 +1179,42 @@ def _interpolate_with_fill(
# We only get here for non-ExtensionBlock
fill_value = convert_scalar_for_putitemlike(fill_value, self.values.dtype)

values = missing.interpolate_2d(
values,
interpolate_2d = functools.partial(
missing.interpolate_2d,
method=method,
axis=axis,
limit=limit,
fill_value=fill_value,
dtype=self.dtype,
)

if limit_area is None:
values = interpolate_2d(values, axis=axis)
else:
def func(values):
invalid = isna(values)

if not invalid.any():
return values

if not invalid.all():
first = missing.find_valid_index(values, "first")
last = missing.find_valid_index(values, "last")

values = interpolate_2d(values)

if limit_area == "inside":
invalid[first : last + 1] = False
elif limit_area == "outside":
invalid[:first] = False
invalid[last + 1 :] = False

values[invalid] = np.nan
else:
values = interpolate_2d(values)
return values

values = np.apply_along_axis(func, axis, values)

blocks = [self.make_block_same_class(values, ndim=self.ndim)]
return self._maybe_downcast(blocks, downcast)

Expand Down

0 comments on commit 92148ff

Please sign in to comment.