Skip to content

Commit f7537eb

Browse files
aileronajayischurov
authored andcommitted
ENH: add timedelta as valid type for interpolate with method='time' (pandas-dev#14799)
1 parent 2583420 commit f7537eb

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Other enhancements
5959
- ``Series`` provides a ``to_excel`` method to output Excel files (:issue:`8825`)
6060
- The ``usecols`` argument in ``pd.read_csv`` now accepts a callable function as a value (:issue:`14154`)
6161
- ``pd.DataFrame.plot`` now prints a title above each subplot if ``suplots=True`` and ``title`` is a list of strings (:issue:`14753`)
62+
- ``pd.Series.interpolate`` now supports timedelta as an index type with ``method='time'`` (:issue:`6424`)
6263

6364
.. _whatsnew_0200.api_breaking:
6465

pandas/core/missing.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
is_float_dtype, is_datetime64_dtype,
1313
is_integer_dtype, _ensure_float64,
1414
is_scalar,
15-
_DATELIKE_DTYPES)
15+
_DATELIKE_DTYPES,
16+
needs_i8_conversion)
1617
from pandas.types.missing import isnull
1718

1819

@@ -187,7 +188,7 @@ def _interp_limit(invalid, fw_limit, bw_limit):
187188
if method in ('values', 'index'):
188189
inds = np.asarray(xvalues)
189190
# hack for DatetimeIndex, #1646
190-
if issubclass(inds.dtype.type, np.datetime64):
191+
if needs_i8_conversion(inds.dtype.type):
191192
inds = inds.view(np.int64)
192193
if inds.dtype == np.object_:
193194
inds = lib.maybe_convert_objects(inds)

pandas/tests/series/test_missing.py

+17
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,23 @@ def test_spline_error(self):
891891
with tm.assertRaises(ValueError):
892892
s.interpolate(method='spline', order=0)
893893

894+
def test_interp_timedelta64(self):
895+
# GH 6424
896+
df = Series([1, np.nan, 3],
897+
index=pd.to_timedelta([1, 2, 3]))
898+
result = df.interpolate(method='time')
899+
expected = Series([1., 2., 3.],
900+
index=pd.to_timedelta([1, 2, 3]))
901+
assert_series_equal(result, expected)
902+
903+
# test for non uniform spacing
904+
df = Series([1, np.nan, 3],
905+
index=pd.to_timedelta([1, 2, 4]))
906+
result = df.interpolate(method='time')
907+
expected = Series([1., 1.666667, 3.],
908+
index=pd.to_timedelta([1, 2, 4]))
909+
assert_series_equal(result, expected)
910+
894911

895912
if __name__ == '__main__':
896913
import nose

0 commit comments

Comments
 (0)