Skip to content

Commit

Permalink
ENH: support NaT values into datetime series for interpolation (panda…
Browse files Browse the repository at this point in the history
  • Loading branch information
atulagrwl committed Jul 11, 2018
1 parent 5d0daa0 commit 862d62d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Other Enhancements
- :meth:`Series.nlargest`, :meth:`Series.nsmallest`, :meth:`DataFrame.nlargest`, and :meth:`DataFrame.nsmallest` now accept the value ``"all"`` for the ``keep`` argument. This keeps all ties for the nth largest/smallest value (:issue:`16818`)
- :class:`IntervalIndex` has gained the :meth:`~IntervalIndex.set_closed` method to change the existing ``closed`` value (:issue:`21670`)
- :func:`~DataFrame.to_csv` and :func:`~DataFrame.to_json` now support ``compression='infer'`` to infer compression based on filename (:issue:`15008`)
- Implement interpolating ``NaT`` values in ``datetime`` series (:issue:`11701`)
-

.. _whatsnew_0240.api_breaking:
Expand Down
13 changes: 13 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6112,6 +6112,14 @@ def interpolate(self, method='linear', axis=0, limit=None, inplace=False,
raise NotImplementedError("Interpolation with NaNs in the index "
"has not been implemented. Try filling "
"those NaNs before interpolating.")
is_datetime = False
datetime_timezone = None
if is_datetime64_any_dtype(_maybe_transposed_self):
_datetime_nat_values = _maybe_transposed_self.isnull()
datetime_timezone = _maybe_transposed_self.dt.tz
_maybe_transposed_self = _maybe_transposed_self.astype('int')
_maybe_transposed_self[_datetime_nat_values] = np.nan
is_datetime = True
data = _maybe_transposed_self._data
new_data = data.interpolate(method=method, axis=ax, index=index,
values=_maybe_transposed_self, limit=limit,
Expand All @@ -6120,6 +6128,11 @@ def interpolate(self, method='linear', axis=0, limit=None, inplace=False,
inplace=inplace, downcast=downcast,
**kwargs)

if is_datetime:
new_data = self._constructor(new_data)
new_data = pd.to_datetime(new_data, utc=True).dt.tz_convert(
datetime_timezone)

if inplace:
if axis == 1:
new_data = self._constructor(new_data).T._data
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1317,3 +1317,23 @@ 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)

def test_series_interpolate_nat_inplace(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.interpolate(inplace=True)
tm.assert_series_equal(result, expected)

0 comments on commit 862d62d

Please sign in to comment.