Skip to content

ENH: support NaT values into datetime series for interpolation (#11701) #21851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ Other Enhancements
- :func:`to_timedelta` now supports iso-formated timedelta strings (:issue:`21877`)
- :class:`Series` and :class:`DataFrame` now support :class:`Iterable` in constructor (:issue:`2193`)
- :class:`DatetimeIndex` gained :attr:`DatetimeIndex.timetz` attribute. Returns local time with timezone information. (:issue:`21358`)
- Implement interpolating ``NaT`` values in ``datetime`` series (:issue:`11701`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wording here is awkward. It doesn't need to be a complete sentence per se, but take a look at the other entries for examples.

-

.. _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 @@ -6316,6 +6316,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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be pushed down to the block in pandas/core/internals, should go with def _interpolate I think

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to explore more about it. Will do that in a day or two.

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 @@ -6324,6 +6332,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
13 changes: 13 additions & 0 deletions pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1317,3 +1317,16 @@ def test_series_interpolate_intraday(self):
result = ts.reindex(new_index).interpolate(method='time')

tm.assert_numpy_array_equal(result.values, exp.values)

@pytest.mark.parametrize("inplace", [True, False])
def test_series_interpolate_nat(self, tz_naive_fixture, inplace):
# GH 11701
expected = pd.Series(pd.date_range('2015-01-01',
'2015-01-30', tz=tz_naive_fixture))
result = expected.copy()
result[[3, 4, 5, 13, 14, 15]] = pd.NaT
if inplace:
result.interpolate(inplace=inplace)
else:
result = result.interpolate()
tm.assert_series_equal(result, expected)