Skip to content

[WIP] fork of #31048 for CI testing DO NOT MERGE #34749

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
30 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
8f89508
Merge remote-tracking branch 'upstream/master' into fix_interpolate_l…
simonjayhawkins Jun 12, 2020
e3f0ab5
Merge remote-tracking branch 'upstream/master' into fix_interpolate_l…
simonjayhawkins Jun 13, 2020
4af354d
do not dispatch to fillna
simonjayhawkins Jun 13, 2020
39c2222
Merge remote-tracking branch 'upstream/master' into refactor-interpol…
simonjayhawkins Jun 14, 2020
17933a6
remove interpolate_1d_fill
simonjayhawkins Jun 14, 2020
e739841
Merge remote-tracking branch 'upstream/master' into refactor-interpol…
simonjayhawkins Jun 14, 2020
2f4e06b
doc fix
simonjayhawkins Jun 14, 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.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ Indexing
Missing
^^^^^^^
- Calling :meth:`fillna` on an empty Series now correctly returns a shallow copied object. The behaviour is now consistent with :class:`Index`, :class:`DataFrame` and a non-empty :class:`Series` (:issue:`32543`).
- Bug in :meth:`Series.interpolate` where kwarg ``limit_area`` had no effect when using methods ``pad``, ``ffill``, ``backfill`` and ``bfill`` (:issue:`26796`)
- Bug in :meth:`replace` when argument ``to_replace`` is of type dict/list and is used on a :class:`Series` containing ``<NA>`` was raising a ``TypeError``. The method now handles this by ignoring ``<NA>`` values when doing the comparison for the replacement (:issue:`32621`)
- Bug in :meth:`~Series.any` and :meth:`~Series.all` incorrectly returning ``<NA>`` for all ``False`` or all ``True`` values using the nulllable boolean dtype and with ``skipna=False`` (:issue:`33253`)
- Clarified documentation on interpolate with method =akima. The ``der`` parameter must be scalar or None (:issue:`33426`)
Expand Down
39 changes: 35 additions & 4 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 @@ -1091,7 +1092,7 @@ def interpolate(
if (self.is_bool or self.is_integer) and not self.is_timedelta:
return self if inplace else self.copy()

# a fill na type method
# a fillna type method
try:
m = missing.clean_fill_method(method)
except ValueError:
Expand All @@ -1103,6 +1104,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 @@ -1131,6 +1133,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 @@ -1152,15 +1155,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:
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
64 changes: 64 additions & 0 deletions pandas/tests/series/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,70 @@ def test_interp_limit_area(self):
with pytest.raises(ValueError, match=msg):
s.interpolate(method="linear", limit_area="abc")

def test_interp_limit_area_with_pad(self):
# https://github.com/pandas-dev/pandas/issues/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):
# https://github.com/pandas-dev/pandas/issues/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)

@pytest.mark.parametrize("method", ["backfill", "bfill", "pad", "ffill"])
@pytest.mark.parametrize("limit_area", ["inside", "outside", "both", None])
def test_interp_limit_area_all_missing(self, method, limit_area):
# https://github.com/pandas-dev/pandas/issues/26796
ser = Series([np.nan, np.nan, np.nan])
result = ser.interpolate(method=method, limit_area=limit_area)
tm.assert_series_equal(result, ser)

@pytest.mark.parametrize("method", ["backfill", "bfill", "pad", "ffill"])
@pytest.mark.parametrize("limit_area", ["inside", "outside", "both", None])
def test_interp_limit_area_none_missing(self, method, limit_area):
# https://github.com/pandas-dev/pandas/issues/26796
ser = Series([1, 2, 3])
result = ser.interpolate(method=method, limit_area=limit_area)
tm.assert_series_equal(result, ser)

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