Skip to content

BUG: Fix computation of invalid NaN indexes for interpolate. #11124

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

Merged
merged 1 commit into from
Sep 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ Other enhancements

- ``DataFrame`` has gained the ``nlargest`` and ``nsmallest`` methods (:issue:`10393`)

- Add a ``limit_direction`` keyword argument that works with ``limit`` to enable ``interpolate`` to fill ``NaN`` values forward, backward, or both (:issue:`9218` and :issue:`10420`)
- Add a ``limit_direction`` keyword argument that works with ``limit`` to enable ``interpolate`` to fill ``NaN`` values forward, backward, or both (:issue:`9218`, :issue:`10420`, :issue:`11115`)

.. ipython:: python

Expand Down
13 changes: 5 additions & 8 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,13 +1582,10 @@ def interpolate_1d(xvalues, yvalues, method='linear', limit=None,
method = 'values'

def _interp_limit(invalid, fw_limit, bw_limit):
"Get idx of values that won't be forward-filled b/c they exceed the limit."
all_nans = np.where(invalid)[0]
if all_nans.size == 0: # no nans anyway
return []
violate = [invalid[max(0, x - bw_limit):x + fw_limit + 1] for x in all_nans]
violate = np.array([x.all() & (x.size > bw_limit + fw_limit) for x in violate])
return all_nans[violate] + fw_limit - bw_limit
"Get idx of values that won't be filled b/c they exceed the limits."
for x in np.where(invalid)[0]:
if invalid[max(0, x - fw_limit):x + bw_limit + 1].all():
yield x

valid_limit_directions = ['forward', 'backward', 'both']
limit_direction = limit_direction.lower()
Expand Down Expand Up @@ -1624,7 +1621,7 @@ def _interp_limit(invalid, fw_limit, bw_limit):
if limit_direction == 'backward':
violate_limit = sorted(end_nans | set(_interp_limit(invalid, 0, limit)))
if limit_direction == 'both':
violate_limit = _interp_limit(invalid, limit, limit)
violate_limit = sorted(_interp_limit(invalid, limit, limit))

xvalues = getattr(xvalues, 'values', xvalues)
yvalues = getattr(yvalues, 'values', yvalues)
Expand Down
20 changes: 19 additions & 1 deletion pandas/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,6 @@ def test_interp_limit_forward(self):

def test_interp_limit_bad_direction(self):
s = Series([1, 3, np.nan, np.nan, np.nan, 11])
expected = Series([1., 3., 5., 7., 9., 11.])

self.assertRaises(ValueError, s.interpolate,
method='linear', limit=2,
Expand Down Expand Up @@ -930,6 +929,25 @@ def test_interp_limit_to_ends(self):
method='linear', limit=2, limit_direction='both')
assert_series_equal(result, expected)

def test_interp_limit_before_ends(self):
# These test are for issue #11115 -- limit ends properly.
s = Series([np.nan, np.nan, 5, 7, np.nan, np.nan])

expected = Series([np.nan, np.nan, 5., 7., 7., np.nan])
result = s.interpolate(
method='linear', limit=1, limit_direction='forward')
assert_series_equal(result, expected)

expected = Series([np.nan, 5., 5., 7., np.nan, np.nan])
result = s.interpolate(
method='linear', limit=1, limit_direction='backward')
assert_series_equal(result, expected)

expected = Series([np.nan, 5., 5., 7., 7., np.nan])
result = s.interpolate(
method='linear', limit=1, limit_direction='both')
assert_series_equal(result, expected)

def test_interp_all_good(self):
# scipy
tm._skip_if_no_scipy()
Expand Down