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 1 commit
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
Prev Previous commit
Next Next commit
clean up
* black formatting
* typo
  • Loading branch information
cchwala committed Jan 15, 2020
commit 298032561d920594eb2f672feff44dda2fdef408
3 changes: 1 addition & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6724,8 +6724,7 @@ def interpolate(

# Set `limit_direction` depending on `method`
Copy link
Contributor

Choose a reason for hiding this comment

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

is there a reason this logic does not simply belong in interpolate_2d?

Copy link
Contributor

Choose a reason for hiding this comment

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

or call something like clean_fill_method

Copy link
Contributor Author

@cchwala cchwala Feb 18, 2020

Choose a reason for hiding this comment

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

limit_direction has no effect in interpolate_2d.

My idea was to detect conflicting user input, e.g. pad with limit_direction='backward' as early as possible in the chain of calls to the many functions that are involved.

But I can move the logic to a function like clean_fill_method, e.g. called set_limit_direction. Should this function belong to generic.py or missing.py?

Copy link
Member

Choose a reason for hiding this comment

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

interpolate_2d is also called from fillna. only NDFrame.interpolate allows limit_direction so does make sense to validate here.

clean_fill_method checks method. probably want to avoid passing more parameters around.

Copy link
Contributor

Choose a reason for hiding this comment

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

pls make a method similr to clean_fill_method then, e.g this logic should living pandas/core/missing.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure what to do here. after merge with master, there is similar code at this location, but it is not mine...

if (method == "pad") or (method == "ffill"):
if (limit_direction == "backward") or (
limit_direction == "both"):
if (limit_direction == "backward") or (limit_direction == "both"):
raise ValueError(
f"`limit_direction` must not be `{limit_direction}` "
f"for method `{method}`"
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,7 @@ def _interpolate_with_fill(
# works by applying the fill along a certain axis.
# 2. All other cases: Then, `missing.interpolate_2d()` can be used.
if limit_area is not None:

def func(x):
return missing.interpolate_1d_fill(
x,
Expand All @@ -1186,7 +1187,8 @@ def func(x):
fill_value=fill_value,
dtype=self.dtype,
)
# Beware that this also change the input array `values`!

# Beware that this also changes the input array `values`!
interp_values = np.apply_along_axis(func, axis, values)
else:
interp_values = missing.interpolate_2d(
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def interpolate_1d(


def _derive_indices_of_nans_to_preserve(
yvalues, valid, invalid, limit, limit_area, limit_direction,
yvalues, valid, invalid, limit, limit_area, limit_direction
):
""" Derive the indices of NaNs that shall be preserved after interpolation
This function is called by `interpolate_1d` and takes the arguments with
Expand Down
18 changes: 6 additions & 12 deletions pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,11 +1384,9 @@ def test_interp_limit_area(self):

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])
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])
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)

Expand All @@ -1398,8 +1396,7 @@ def test_interp_limit_area_with_pad(self):
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])
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)

Expand All @@ -1411,11 +1408,9 @@ def test_interp_limit_area_with_pad(self):

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])
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])
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)

Expand All @@ -1425,8 +1420,7 @@ def test_interp_limit_area_with_backfill(self):
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])
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)

Expand Down