diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index d89103309e990..f534c67273560 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -59,6 +59,7 @@ Other enhancements - ``Series`` provides a ``to_excel`` method to output Excel files (:issue:`8825`) - The ``usecols`` argument in ``pd.read_csv`` now accepts a callable function as a value (:issue:`14154`) - ``pd.DataFrame.plot`` now prints a title above each subplot if ``suplots=True`` and ``title`` is a list of strings (:issue:`14753`) +- ``pd.Series.interpolate`` now supports timedelta as an index type with ``method='time'`` (:issue:`6424`) .. _whatsnew_0200.api_breaking: diff --git a/pandas/core/missing.py b/pandas/core/missing.py index b847415f274db..f1191ff1c7009 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -12,7 +12,8 @@ is_float_dtype, is_datetime64_dtype, is_integer_dtype, _ensure_float64, is_scalar, - _DATELIKE_DTYPES) + _DATELIKE_DTYPES, + needs_i8_conversion) from pandas.types.missing import isnull @@ -187,7 +188,7 @@ def _interp_limit(invalid, fw_limit, bw_limit): if method in ('values', 'index'): inds = np.asarray(xvalues) # hack for DatetimeIndex, #1646 - if issubclass(inds.dtype.type, np.datetime64): + if needs_i8_conversion(inds.dtype.type): inds = inds.view(np.int64) if inds.dtype == np.object_: inds = lib.maybe_convert_objects(inds) diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index 4e6c58df54dfd..5666a07cad4b8 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -891,6 +891,23 @@ def test_spline_error(self): with tm.assertRaises(ValueError): s.interpolate(method='spline', order=0) + def test_interp_timedelta64(self): + # GH 6424 + df = Series([1, np.nan, 3], + index=pd.to_timedelta([1, 2, 3])) + result = df.interpolate(method='time') + expected = Series([1., 2., 3.], + index=pd.to_timedelta([1, 2, 3])) + assert_series_equal(result, expected) + + # test for non uniform spacing + df = Series([1, np.nan, 3], + index=pd.to_timedelta([1, 2, 4])) + result = df.interpolate(method='time') + expected = Series([1., 1.666667, 3.], + index=pd.to_timedelta([1, 2, 4])) + assert_series_equal(result, expected) + if __name__ == '__main__': import nose