From faf9df00d97fa8179c8df9d242215b62ae20e1dd Mon Sep 17 00:00:00 2001 From: Ka Wo Chen Date: Thu, 25 Jun 2015 02:06:50 -0400 Subject: [PATCH] BUG: GH10365 in interpolate_1d when method is piecewise_polynomial --- doc/source/whatsnew/v0.17.0.txt | 1 + pandas/core/common.py | 4 ++-- pandas/tests/test_generic.py | 41 +++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index 191e777903368..e58a6363bf38d 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -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`) diff --git a/pandas/core/common.py b/pandas/core/common.py index 35b79cecfa996..102decc9d042b 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -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) @@ -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 diff --git a/pandas/tests/test_generic.py b/pandas/tests/test_generic.py index 9a8ec00188d9c..0a9f8dab77372 100644 --- a/pandas/tests/test_generic.py +++ b/pandas/tests/test_generic.py @@ -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()