Skip to content

Commit faf9df0

Browse files
committed
BUG: GH10365 in interpolate_1d when method is piecewise_polynomial
1 parent 383865f commit faf9df0

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

doc/source/whatsnew/v0.17.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,4 @@ Bug Fixes
7878
- Bug in ``ExcelReader`` when worksheet is empty (:issue:`6403`)
7979
- Bug in ``Table.select_column`` where name is not preserved (:issue:`10392`)
8080
- Bug in ``DataFrame.interpolate`` with ``axis=1`` and ``inplace=True`` (:issue:`10395`)
81+
- Bug in ``interpolate_1d`` with ``method='piecewise_polynomial`` (:issue:`10365`)

pandas/core/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,8 +1682,6 @@ def _interp_limit(invalid, limit):
16821682
'piecewise_polynomial', 'pchip']
16831683
if method in sp_methods:
16841684
new_x = new_x[firstIndex:]
1685-
xvalues = xvalues[firstIndex:]
1686-
16871685
result[firstIndex:][invalid] = _interpolate_scipy_wrapper(
16881686
valid_x, valid_y, new_x, method=method, fill_value=fill_value,
16891687
bounds_error=bounds_error, order=order)
@@ -1745,6 +1743,8 @@ def _interpolate_scipy_wrapper(x, y, new_x, method, fill_value=None,
17451743
y = y.copy()
17461744
if not new_x.flags.writeable:
17471745
new_x = new_x.copy()
1746+
if method == "piecewise_polynomial":
1747+
y = y.reshape((-1, 1))
17481748
method = alt_methods[method]
17491749
new_y = method(x, y, new_x)
17501750
return new_y

pandas/tests/test_generic.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,47 @@ def test_interp_limit_no_nans(self):
872872
expected = s
873873
assert_series_equal(result, expected)
874874

875+
def test_interp_piecewise_polynomial(self):
876+
# GH 10365
877+
tm._skip_if_no_scipy()
878+
s1 = Series([1, 2, 3, 4, nan, 6, nan])
879+
s2 = Series([nan, nan, 3, 4, nan, 6, 7])
880+
result1 = s1.interpolate(method='piecewise_polynomial')
881+
result2 = s2.interpolate(method='piecewise_polynomial')
882+
expected1 = Series([1., 2., 3., 4., 5., 6., 7.])
883+
expected2 = Series([nan, nan, 3., 4., 5., 6., 7.])
884+
885+
assert_series_equal(expected1, result1)
886+
assert_series_equal(expected2, result2)
887+
888+
def test_interp_krogh(self):
889+
tm._skip_if_no_scipy()
890+
s1 = Series([0, -2, 0, np.nan], index=[0, 0, 1, 5])
891+
s2 = Series([nan, 0, -2, 0, np.nan], index=[-1, 0, 0, 1, 5])
892+
s3 = Series([nan, 0, 0, 0, np.nan], index=[-1, 0, 0, 0, 5])
893+
result1 = s1.interpolate(method='krogh')
894+
result2 = s2.interpolate(method='krogh')
895+
result3 = s3.interpolate(method='krogh')
896+
expected1 = Series([0., -2., 0., 40.], index=[0, 0, 1, 5])
897+
expected2 = Series([nan, 0., -2., 0., 40.], index=[-1, 0, 0, 1, 5])
898+
expected3 = Series([nan, 0., 0., 0., 0.], index=[-1, 0, 0, 0, 5])
899+
900+
assert_series_equal(expected1, result1)
901+
assert_series_equal(expected2, result2)
902+
assert_series_equal(expected3, result3)
903+
904+
def test_interp_barycentric(self):
905+
tm._skip_if_no_scipy()
906+
s1 = Series([nan, 0, 0, 0, 0, nan])
907+
s2 = Series([nan, 0, 2, 1, nan])
908+
result1 = s1.interpolate(method='barycentric')
909+
result2 = s2.interpolate(method='barycentric')
910+
expected1 = Series([nan, 0., 0., 0., 0., 0.])
911+
expected2 = Series([nan, 0., 2., 1., -3.])
912+
913+
assert_series_equal(expected1, result1)
914+
assert_series_equal(expected2, result2)
915+
875916
def test_describe(self):
876917
_ = self.series.describe()
877918
_ = self.ts.describe()

0 commit comments

Comments
 (0)