Skip to content

BUG: GH10365 in interpolate_1d when method is piecewise_polynomial #10435

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
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@ Bug Fixes
- Bug in ``ExcelReader`` when worksheet is empty (:issue:`6403`)
- Bug in ``Table.select_column`` where name is not preserved (:issue:`10392`)
- Bug in ``DataFrame.interpolate`` with ``axis=1`` and ``inplace=True`` (:issue:`10395`)
- Bug in ``interpolate_1d`` with ``method='piecewise_polynomial`` (:issue:`10365`)
4 changes: 2 additions & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1682,8 +1682,6 @@ def _interp_limit(invalid, limit):
'piecewise_polynomial', 'pchip']
if method in sp_methods:
new_x = new_x[firstIndex:]
xvalues = xvalues[firstIndex:]

result[firstIndex:][invalid] = _interpolate_scipy_wrapper(
valid_x, valid_y, new_x, method=method, fill_value=fill_value,
bounds_error=bounds_error, order=order)
Expand Down Expand Up @@ -1745,6 +1743,8 @@ def _interpolate_scipy_wrapper(x, y, new_x, method, fill_value=None,
y = y.copy()
if not new_x.flags.writeable:
new_x = new_x.copy()
if method == "piecewise_polynomial":
y = y.reshape((-1, 1))
method = alt_methods[method]
new_y = method(x, y, new_x)
return new_y
Expand Down
41 changes: 41 additions & 0 deletions pandas/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,47 @@ def test_interp_limit_no_nans(self):
expected = s
assert_series_equal(result, expected)

def test_interp_piecewise_polynomial(self):
# GH 10365
tm._skip_if_no_scipy()
s1 = Series([1, 2, 3, 4, nan, 6, nan])
s2 = Series([nan, nan, 3, 4, nan, 6, 7])
result1 = s1.interpolate(method='piecewise_polynomial')
result2 = s2.interpolate(method='piecewise_polynomial')
expected1 = Series([1., 2., 3., 4., 5., 6., 7.])
expected2 = Series([nan, nan, 3., 4., 5., 6., 7.])

assert_series_equal(expected1, result1)
assert_series_equal(expected2, result2)

def test_interp_krogh(self):
tm._skip_if_no_scipy()
s1 = Series([0, -2, 0, np.nan], index=[0, 0, 1, 5])
s2 = Series([nan, 0, -2, 0, np.nan], index=[-1, 0, 0, 1, 5])
s3 = Series([nan, 0, 0, 0, np.nan], index=[-1, 0, 0, 0, 5])
result1 = s1.interpolate(method='krogh')
result2 = s2.interpolate(method='krogh')
result3 = s3.interpolate(method='krogh')
expected1 = Series([0., -2., 0., 40.], index=[0, 0, 1, 5])
expected2 = Series([nan, 0., -2., 0., 40.], index=[-1, 0, 0, 1, 5])
expected3 = Series([nan, 0., 0., 0., 0.], index=[-1, 0, 0, 0, 5])

assert_series_equal(expected1, result1)
assert_series_equal(expected2, result2)
assert_series_equal(expected3, result3)

def test_interp_barycentric(self):
tm._skip_if_no_scipy()
s1 = Series([nan, 0, 0, 0, 0, nan])
s2 = Series([nan, 0, 2, 1, nan])
result1 = s1.interpolate(method='barycentric')
result2 = s2.interpolate(method='barycentric')
expected1 = Series([nan, 0., 0., 0., 0., 0.])
expected2 = Series([nan, 0., 2., 1., -3.])

assert_series_equal(expected1, result1)
assert_series_equal(expected2, result2)

def test_describe(self):
_ = self.series.describe()
_ = self.ts.describe()
Expand Down