Skip to content

BUG: Allow DatetimeIndex for scipy interpolate #5977

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
Jan 16, 2014
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
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Bug Fixes
incorrectly (:issue:`5947`)
- Fixed ``to_datetime`` for array with both Tz-aware datetimes and ``NaT``s (:issue:`5961`)
- Bug in rolling skew/kurtosis when passed a Series with bad data (:issue:`5749`)
- Bug in scipy ``interpolate`` methods with a datetime index (:issue: `5975`)

pandas 0.13.0
-------------
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,7 @@ def _interpolate_scipy_wrapper(x, y, new_x, method, fill_value=None,
"""
try:
from scipy import interpolate
from pandas import DatetimeIndex
except ImportError:
raise ImportError('{0} interpolation requires Scipy'.format(method))

Expand All @@ -1413,6 +1414,10 @@ def _interpolate_scipy_wrapper(x, y, new_x, method, fill_value=None,
'piecewise_polynomial': interpolate.piecewise_polynomial_interpolate,
}

if hasattr(x, 'asi8'):
# GH 5975, scipy.interp1d can't hande datetime64s
x, new_x = x.values.view('i8'), new_x.view('i8')

try:
alt_methods['pchip'] = interpolate.pchip_interpolate
except AttributeError:
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,13 @@ def test_interp_nonmono_raise(self):
with tm.assertRaises(ValueError):
s.interpolate(method='krogh')

def test_interp_datetime64(self):
_skip_if_no_scipy()
df = Series([1, np.nan, 3], index=date_range('1/1/2000', periods=3))
result = df.interpolate(method='nearest')
expected = Series([1, 1, 3], index=date_range('1/1/2000', periods=3))
assert_series_equal(result, expected)

class TestDataFrame(tm.TestCase, Generic):
_typ = DataFrame
_comparator = lambda self, x, y: assert_frame_equal(x,y)
Expand Down