diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index dae93feb48b02..37d67e93b86a7 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -163,6 +163,7 @@ Other Enhancements - :func:`Categorical.rename_categories` now accepts a dict-like argument as `new_categories` and only updates the categories found in that dict. (:issue:`17336`) - :func:`read_excel` raises ``ImportError`` with a better message if ``xlrd`` is not installed. (:issue:`17613`) - :meth:`DataFrame.assign` will preserve the original order of ``**kwargs`` for Python 3.6+ users instead of sorting the column names +- Implement interpolating ``NaT`` values in ``datetime`` series (:issue:`11701`) .. _whatsnew_0210.api_breaking: diff --git a/pandas/core/series.py b/pandas/core/series.py index a05324142b223..31825f2aa3e45 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -19,6 +19,7 @@ is_integer, is_integer_dtype, is_float_dtype, is_extension_type, is_datetimetz, + is_datetime64_dtype, is_datetime64tz_dtype, is_timedelta64_dtype, is_list_like, @@ -36,6 +37,7 @@ maybe_cast_to_datetime, maybe_castable) from pandas.core.dtypes.missing import isna, notna, remove_na_arraylike +from pandas.core.tools.datetimes import to_datetime from pandas.core.common import (is_bool_indexer, _default_index, _asarray_tuplesafe, @@ -2734,6 +2736,15 @@ def from_csv(cls, path, sep=',', parse_dates=True, header=None, return result + def interpolate(self, *args, **kwargs): + if (is_datetime64_dtype(self) or + is_datetime64tz_dtype(self)) and self.isnull().any(): + s2 = self.astype('i8').astype('f8') + s2[self.isnull()] = np.nan + return to_datetime(s2.interpolate(*args, **kwargs)) + else: + return super(Series, self).interpolate(*args, **kwargs) + def to_csv(self, path=None, index=True, sep=",", na_rep='', float_format=None, header=False, index_label=None, mode='w', encoding=None, date_format=None, decimal='.'): diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index 01bf7274fd384..3c74c0a8993c8 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -1218,3 +1218,12 @@ def test_series_interpolate_intraday(self): result = ts.reindex(new_index).interpolate(method='time') tm.assert_numpy_array_equal(result.values, exp.values) + + def test_series_interpolate_nat(self): + # GH 11701 + for tz in [None, 'UTC', 'Europe/Paris']: + expected = pd.Series(pd.date_range('2015-01-01', '2015-01-30', tz=tz)) + result = expected.copy() + result[[3, 4, 5, 13, 14, 15]] = pd.NaT + result = result.interpolate() + tm.assert_series_equal(result, expected)