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 1 commit
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
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`)
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 @@ -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
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 @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

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

use tz_native_fixture and paramerize

Choose a reason for hiding this comment

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

Fixed in latest commit.

# 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):
Copy link
Contributor

Choose a reason for hiding this comment

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

same

Copy link
Contributor

Choose a reason for hiding this comment

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

you can parameterize over inplace as well

# 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)