-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs to be pushed down to the block in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use tz_native_fixture and paramerize There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
There was a problem hiding this comment.
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.