Skip to content

Commit 7c1b44c

Browse files
committed
BUG: Implement interpolating NaT values in datetime series
Closes pandas-dev#11701
1 parent db1206a commit 7c1b44c

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ Other Enhancements
163163
- :func:`Categorical.rename_categories` now accepts a dict-like argument as `new_categories` and only updates the categories found in that dict. (:issue:`17336`)
164164
- :func:`read_excel` raises ``ImportError`` with a better message if ``xlrd`` is not installed. (:issue:`17613`)
165165
- :meth:`DataFrame.assign` will preserve the original order of ``**kwargs`` for Python 3.6+ users instead of sorting the column names
166+
- Implement interpolating ``NaT`` values in ``datetime`` series (:issue:`11701`)
166167

167168

168169
.. _whatsnew_0210.api_breaking:

pandas/core/series.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
is_integer, is_integer_dtype,
2020
is_float_dtype,
2121
is_extension_type, is_datetimetz,
22+
is_datetime64_dtype,
2223
is_datetime64tz_dtype,
2324
is_timedelta64_dtype,
2425
is_list_like,
@@ -34,8 +35,10 @@
3435
maybe_upcast, infer_dtype_from_scalar,
3536
maybe_convert_platform,
3637
maybe_cast_to_datetime, maybe_castable)
37-
from pandas.core.dtypes.missing import isna, notna, remove_na_arraylike
38-
38+
from pandas.core.dtypes.missing import (isna, notna,
39+
remove_na_arraylike,
40+
isnull)
41+
from pandas.core.tools.datetimes import to_datetime
3942
from pandas.core.common import (is_bool_indexer,
4043
_default_index,
4144
_asarray_tuplesafe,
@@ -2734,6 +2737,14 @@ def from_csv(cls, path, sep=',', parse_dates=True, header=None,
27342737

27352738
return result
27362739

2740+
def interpolate(self, *args, **kwargs):
2741+
if is_datetime64_dtype(self) and self.isnull().any():
2742+
s2 = self.astype('i8').astype('f8')
2743+
s2[self.isnull()] = np.nan
2744+
return to_datetime(s2.interpolate(*args, **kwargs))
2745+
else:
2746+
return super(Series, self).interpolate(*args, **kwargs)
2747+
27372748
def to_csv(self, path=None, index=True, sep=",", na_rep='',
27382749
float_format=None, header=False, index_label=None,
27392750
mode='w', encoding=None, date_format=None, decimal='.'):

pandas/tests/series/test_missing.py

+10
Original file line numberDiff line numberDiff line change
@@ -1218,3 +1218,13 @@ def test_series_interpolate_intraday(self):
12181218
result = ts.reindex(new_index).interpolate(method='time')
12191219

12201220
tm.assert_numpy_array_equal(result.values, exp.values)
1221+
1222+
def test_series_interpolate_nat(self):
1223+
# GH 11701
1224+
expected = pd.Series(pd.date_range('2015-01-01', '2015-01-30'))
1225+
result = expected.copy()
1226+
result[[3, 4, 5, 13, 14, 15]] = pd.NaT
1227+
result = result.interpolate()
1228+
print(result)
1229+
print(expected)
1230+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)