From bd17ead0a961f8362b792c178803971e3f25dbcf Mon Sep 17 00:00:00 2001 From: "Graham R. Jeffries" Date: Tue, 12 Aug 2014 19:11:00 -0400 Subject: [PATCH 1/2] changes to avoid extrapolating over missing data This commit changes `np.interp()` arguments to include the default values of the left and right parameters as np.nan. In effect, when pandas interpolates a Series with trailing missing data, missing data values are preserved rather than being overwritten with the default value (last non-missing value). --- pandas/core/common.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index 48fb75f59ac34..07b0b8da55786 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -1543,7 +1543,8 @@ def _interp_limit(invalid, limit): inds = inds[firstIndex:] result[firstIndex:][invalid] = np.interp(inds[invalid], inds[valid], - yvalues[firstIndex:][valid]) + yvalues[firstIndex:][valid], + np.nan, np.nan) if limit: result[violate_limit] = np.nan From 760f6be43d2f4e63f5fa3fcaf9e4a13177fc0ed0 Mon Sep 17 00:00:00 2001 From: "Graham R. Jeffries" Date: Tue, 12 Aug 2014 19:21:10 -0400 Subject: [PATCH 2/2] test that interpolate doesn't extrapolate Added a test that confirms that linear interpolation of a Series does not extrapolate over missing data that trails the last known value. --- pandas/tests/test_common.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/tests/test_common.py b/pandas/tests/test_common.py index 5e91adbe1a2fa..d909420e107e7 100644 --- a/pandas/tests/test_common.py +++ b/pandas/tests/test_common.py @@ -389,6 +389,12 @@ def test_groupby(): for k, v in grouped: assert v == expected[k] + + +def test_interpolate_linear(): + a = Series([np.nan, 1, np.nan, 3, np.nan])) + b = a.interpolate() + assert(b[4] == np.nan) def test_is_list_like():