Skip to content

FIX: fix interpolate with kwarg limit area and limit direction using pad or bfill #31048

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
9afe992
Added failing test for https://github.com/pandas-dev/pandas/issues/26796
cchwala Jan 15, 2020
3a191b9
Added implementation to support `limit_area`
cchwala Jan 15, 2020
fd5d8e8
fix test
cchwala Jan 15, 2020
26d88ed
pep8
cchwala Jan 15, 2020
6597aca
fixed small error that actually had no effect since the input array `…
cchwala Jan 15, 2020
c536d3c
Raise when forbidden combination of `method` and `limit_direction` ar…
cchwala Jan 15, 2020
ed9cf21
Updated docstring with info about allowed combinations of `method` an…
cchwala Jan 15, 2020
2980325
clean up
cchwala Jan 15, 2020
ecf428e
Added entry to whatsnew file
cchwala Jan 15, 2020
f8a3423
Removed `axis` kwarg from `interpolate_1d_fill` because it was unused
cchwala Jan 15, 2020
6733186
Type annotations added to new function `interpolate_1d_fill`
cchwala Jan 15, 2020
c5b77d2
fixed incorrectly sorted imports
cchwala Jan 15, 2020
0bb36de
Added type annotation, updated docstring and removed unnecessary argu…
cchwala Feb 5, 2020
a467afd
Reverting docstring entry for default value of `limit_direction`
cchwala Feb 18, 2020
5466d8c
Moved logic for calling `missing.interpolate_1d_fill` to `missing.int…
cchwala Feb 18, 2020
3e968fc
Moved whatsnew entry to v1.1.0.rst
cchwala Feb 18, 2020
556a3cf
clean up
cchwala Feb 18, 2020
6c1e429
fixed missing Optional in type definition
cchwala Feb 18, 2020
767b0ca
small fix so that CI type validation does not complain
cchwala Mar 16, 2020
b82aaff
Merge remote-tracking branch 'upstream/master' into fix_interpolate_l…
cchwala Mar 17, 2020
26ef7b5
Apply suggestions from code review concerning list instead of set
cchwala Mar 19, 2020
b4b6b5a
added import for missing List type
cchwala Mar 19, 2020
e259549
fixed unsorted order of imports
cchwala Mar 19, 2020
8ceff58
Merge remote-tracking branch 'upstream/master' into fix_interpolate_l…
cchwala Aug 25, 2020
7c5ad7d
Added tests back in
cchwala Aug 25, 2020
92148ff
Added new solution to account for limit_area with pad
cchwala Aug 26, 2020
d62e02e
black formatting
cchwala Aug 26, 2020
c2473f2
added whatsnew entry
cchwala Aug 26, 2020
610e347
Test with moving logic for interpolate_2d with `limite_area` directly…
cchwala Sep 14, 2020
570e3c2
fix wrong arg order by using kwargs as suggested in https://github.co…
cchwala Sep 15, 2020
721304a
Added comment to explain recursion and added typing for interpolate_2d
cchwala Sep 15, 2020
73ab1bf
improved test code coverage
cchwala Sep 15, 2020
a33f629
fixed wrong typing
cchwala Sep 15, 2020
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/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ Missing
^^^^^^^

- Bug in :meth:`SeriesGroupBy.transform` now correctly handles missing values for `dropna=False` (:issue:`35014`)
- Bug in :meth:`Series.interpolate` where kwarg ``limit_area`` and ``limit_direction`` had no effect when using methods ``pad`` and ``backfill`` (:issue:`31048`)
-

MultiIndex
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1693,9 +1693,9 @@ def fillna(self, value=None, method=None, limit=None):

# TODO: dispatch when self.categories is EA-dtype
values = np.asarray(self).reshape(-1, len(self))
values = interpolate_2d(values, method, 0, None, value).astype(
self.categories.dtype
)[0]
values = interpolate_2d(
values, method=method, axis=0, limit=None, fill_value=value,
).astype(self.categories.dtype)[0]
codes = _get_codes_for_values(values, self.categories)

else:
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,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 +1156,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 @@ -1181,6 +1183,7 @@ def _interpolate_with_fill(
method=method,
axis=axis,
limit=limit,
limit_area=limit_area,
fill_value=fill_value,
dtype=self.dtype,
)
Expand Down
53 changes: 52 additions & 1 deletion pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy as np

from pandas._libs import algos, lib
from pandas._typing import Axis, Dtype, Hashable
from pandas.compat._optional import import_optional_dependency

from pandas.core.dtypes.cast import infer_dtype_from_array
Expand Down Expand Up @@ -543,12 +544,62 @@ def _cubicspline_interpolate(xi, yi, x, axis=0, bc_type="not-a-knot", extrapolat


def interpolate_2d(
values, method="pad", axis=0, limit=None, fill_value=None, dtype=None
values,
method: str = "pad",
axis: Axis = 0,
limit: Optional[int] = None,
limit_area: Optional[str] = None,
fill_value: Optional[Hashable] = None,
dtype: Optional[Dtype] = None,
):
"""
Perform an actual interpolation of values, values will be make 2-d if
needed fills inplace, returns the result.
"""

# `limit_area` is not supported by `pad_2d` and `backfill_2d`. Hence, the
# following code block does a recursive call and applies the interpolation
# and `limit_area` logic along a certain axis.
if limit_area is not None:

def func(values):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this a module level function and just call it here

invalid = isna(values)

if not invalid.any():
return values

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

values = interpolate_2d(
values,
method=method,
limit=limit,
fill_value=fill_value,
dtype=dtype,
)

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,
method=method,
limit=limit,
fill_value=fill_value,
dtype=dtype,
)
return values

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

orig_values = values

transf = (lambda x: x) if axis == 0 else (lambda x: x.T)
Expand Down
60 changes: 60 additions & 0 deletions pandas/tests/series/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,66 @@ def test_interp_limit_direction_raises(self, method, limit_direction, expected):
with pytest.raises(ValueError, match=msg):
s.interpolate(method=method, limit_direction=limit_direction)

def test_interp_limit_area_with_pad(self):
# Test for issue #26796
s = Series([np.nan, np.nan, 3, np.nan, np.nan, np.nan, 7, np.nan, np.nan])

expected = Series([np.nan, np.nan, 3.0, 3.0, 3.0, 3.0, 7.0, np.nan, np.nan])
result = s.interpolate(method="pad", limit_area="inside")
tm.assert_series_equal(result, expected)

expected = Series(
[np.nan, np.nan, 3.0, 3.0, np.nan, np.nan, 7.0, np.nan, np.nan]
)
result = s.interpolate(method="pad", limit_area="inside", limit=1)
tm.assert_series_equal(result, expected)

expected = Series([np.nan, np.nan, 3.0, np.nan, np.nan, np.nan, 7.0, 7.0, 7.0])
result = s.interpolate(method="pad", limit_area="outside")
tm.assert_series_equal(result, expected)

expected = Series(
[np.nan, np.nan, 3.0, np.nan, np.nan, np.nan, 7.0, 7.0, np.nan]
)
result = s.interpolate(method="pad", limit_area="outside", limit=1)
tm.assert_series_equal(result, expected)

# Test for all NaNs
s = Series([np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])
expected = Series([np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])
result = s.interpolate(method="pad", limit_area="outside", limit=1)
tm.assert_series_equal(result, expected)

# Test for no NaNs
s = Series([1, 2, 3, 4])
expected = Series([1, 2, 3, 4])
result = s.interpolate(method="pad", limit_area="outside", limit=1)
tm.assert_series_equal(result, expected)

def test_interp_limit_area_with_backfill(self):
# Test for issue #26796
s = Series([np.nan, np.nan, 3, np.nan, np.nan, np.nan, 7, np.nan, np.nan])

expected = Series([np.nan, np.nan, 3.0, 7.0, 7.0, 7.0, 7.0, np.nan, np.nan])
result = s.interpolate(method="bfill", limit_area="inside")
tm.assert_series_equal(result, expected)

expected = Series(
[np.nan, np.nan, 3.0, np.nan, np.nan, 7.0, 7.0, np.nan, np.nan]
)
result = s.interpolate(method="bfill", limit_area="inside", limit=1)
tm.assert_series_equal(result, expected)

expected = Series([3.0, 3.0, 3.0, np.nan, np.nan, np.nan, 7.0, np.nan, np.nan])
result = s.interpolate(method="bfill", limit_area="outside")
tm.assert_series_equal(result, expected)

expected = Series(
[np.nan, 3.0, 3.0, np.nan, np.nan, np.nan, 7.0, np.nan, np.nan]
)
result = s.interpolate(method="bfill", limit_area="outside", limit=1)
tm.assert_series_equal(result, expected)

def test_interp_limit_direction(self):
# These tests are for issue #9218 -- fill NaNs in both directions.
s = Series([1, 3, np.nan, np.nan, np.nan, 11])
Expand Down