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 28 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
37 changes: 34 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,43 @@ 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:
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 put this in a function in missing and just call here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay. But if I do this I could also directly integrate it into missing.interpolate_2d, e.g. like in this branch. Advantage, no need to introduce yet another function that does some interpolation. Disadvantage, obfuscates missing.interpolate_2d. For this, adding some comments would help for sure.

If you prefer the separate function in missing, what function name would you choose?

Copy link
Member

Choose a reason for hiding this comment

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

e.g. like in this branch.

I would merge recursive solution here for review (so that ci has been run against it, i think needs changes to pandas/core/arrays/categorical.py see master...simonjayhawkins:fix_interpolate_limit_area_and_limit_direction_with_pad)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay. Merged and CI is running.

I have also pushed a branch with an example implementation using a separate function in missing. The name of the new function is not good, but the implementation would probably look something like this.

For both solutions, tests for interpolation pass, but I did not run the full test suite. Will see what changes are required when CI is done.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test are green.

If you find the recursive solution appropriate, I would add a comment to the function that briefly explains the reasoning. Anything else that needs improvement?

Copy link
Member

Choose a reason for hiding this comment

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

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
48 changes: 48 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,54 @@ 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)

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