Skip to content

Commit

Permalink
BUG: Implement interpolating NaT values in datetime series
Browse files Browse the repository at this point in the history
  • Loading branch information
s-celles committed Sep 28, 2017
1 parent db1206a commit 7c1b44c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 13 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -34,8 +35,10 @@
maybe_upcast, infer_dtype_from_scalar,
maybe_convert_platform,
maybe_cast_to_datetime, maybe_castable)
from pandas.core.dtypes.missing import isna, notna, remove_na_arraylike

from pandas.core.dtypes.missing import (isna, notna,
remove_na_arraylike,
isnull)
from pandas.core.tools.datetimes import to_datetime
from pandas.core.common import (is_bool_indexer,
_default_index,
_asarray_tuplesafe,
Expand Down Expand Up @@ -2734,6 +2737,14 @@ def from_csv(cls, path, sep=',', parse_dates=True, header=None,

return result

def interpolate(self, *args, **kwargs):
if is_datetime64_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='.'):
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1218,3 +1218,13 @@ 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
expected = pd.Series(pd.date_range('2015-01-01', '2015-01-30'))
result = expected.copy()
result[[3, 4, 5, 13, 14, 15]] = pd.NaT
result = result.interpolate()
print(result)
print(expected)
tm.assert_series_equal(result, expected)

0 comments on commit 7c1b44c

Please sign in to comment.