-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Fix (Series|DataFrame).interpolate for datetime dtypes #19291
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 4 commits
1fef1f1
c652a55
a759489
e3b6f39
55df7e0
0115ef1
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 |
---|---|---|
|
@@ -1106,7 +1106,7 @@ def check_int_bool(self, inplace): | |
# a fill na type method | ||
try: | ||
m = missing.clean_fill_method(method) | ||
except: | ||
except ValueError: | ||
m = None | ||
|
||
if m is not None: | ||
|
@@ -1121,7 +1121,7 @@ def check_int_bool(self, inplace): | |
# try an interp method | ||
try: | ||
m = missing.clean_interp_method(method, **kwargs) | ||
except: | ||
except ValueError: | ||
m = None | ||
|
||
if m is not None: | ||
|
@@ -1180,24 +1180,9 @@ def _interpolate(self, method=None, index=None, values=None, | |
if fill_value is None: | ||
fill_value = self.fill_value | ||
|
||
if method in ('krogh', 'piecewise_polynomial', 'pchip'): | ||
if not index.is_monotonic: | ||
raise ValueError("{0} interpolation requires that the " | ||
"index be monotonic.".format(method)) | ||
# process 1-d slices in the axis direction | ||
|
||
def func(x): | ||
|
||
# process a 1-d slice, returning it | ||
# should the axis argument be handled below in apply_along_axis? | ||
# i.e. not an arg to missing.interpolate_1d | ||
return missing.interpolate_1d(index, x, method=method, limit=limit, | ||
limit_direction=limit_direction, | ||
fill_value=fill_value, | ||
bounds_error=False, **kwargs) | ||
|
||
# interp each column independently | ||
interp_values = np.apply_along_axis(func, axis, data) | ||
interp_values = _interpolate_values(method, data, index, axis, | ||
limit, limit_direction, | ||
fill_value, **kwargs) | ||
|
||
blocks = [self.make_block(interp_values, klass=self.__class__, | ||
fastpath=True)] | ||
|
@@ -2592,6 +2577,44 @@ def set(self, locs, values, check=False): | |
|
||
self.values[locs] = values | ||
|
||
def _interpolate(self, method=None, index=None, values=None, | ||
fill_value=None, axis=0, limit=None, | ||
limit_direction='forward', inplace=False, downcast=None, | ||
mgr=None, **kwargs): | ||
""" interpolate using scipy wrappers, adapted to datetime64 values""" | ||
|
||
inplace = validate_bool_kwarg(inplace, 'inplace') | ||
data = self.values if inplace else self.values.copy() | ||
|
||
# only deal with floats | ||
mask = isna(self.values) | ||
if self.is_datetimetz: | ||
# Convert to UTC for interpolation | ||
data = data.tz_convert('UTC').values | ||
if self.ndim > 1: | ||
# DataFrame | ||
data = np.atleast_2d(data) | ||
mask = np.atleast_2d(mask) | ||
data = data.astype(np.float64) | ||
data[mask] = np.nan | ||
|
||
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. ths is a hodgepodge. use the hierarchy to make this code readable. IOW put the datetime stuff in the datetimeblock. 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 is in the DatetimeBlock. I'm confused. 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. then why would you need is_datetime. 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 be way less if/then stuff. 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.
It's specifically |
||
if fill_value is None: | ||
fill_value = self.fill_value | ||
|
||
interp_values = _interpolate_values(method, data, index, axis, | ||
limit, limit_direction, | ||
fill_value, **kwargs) | ||
if self.is_datetimetz: | ||
interp_values = interp_values.squeeze() | ||
utc_values = self._holder(interp_values, tz='UTC') | ||
interp_values = utc_values.tz_convert(self.values.tz) | ||
else: | ||
interp_values = interp_values.astype(self.dtype) | ||
|
||
blocks = [self.make_block(interp_values, klass=self.__class__, | ||
fastpath=True)] | ||
return self._maybe_downcast(blocks, downcast) | ||
|
||
|
||
class DatetimeTZBlock(NonConsolidatableMixIn, DatetimeBlock): | ||
""" implement a datetime64 block with a tz attribute """ | ||
|
@@ -5675,3 +5698,26 @@ def _preprocess_slice_or_indexer(slice_or_indexer, length, allow_fill): | |
if not allow_fill: | ||
indexer = maybe_convert_indices(indexer, length) | ||
return 'fancy', indexer, len(indexer) | ||
|
||
|
||
def _interpolate_values(method, data, index, axis, limit, limit_direction, | ||
fill_value, **kwargs): | ||
"""interpolate using scipy wrappers""" | ||
if method in ('krogh', 'piecewise_polynomial', 'pchip'): | ||
if not index.is_monotonic: | ||
raise ValueError("{0} interpolation requires that the " | ||
"index be monotonic.".format(method)) | ||
# process 1-d slices in the axis direction | ||
|
||
def func(x): | ||
# process a 1-d slice, returning it | ||
# should the axis argument be handled below in apply_along_axis? | ||
# i.e. not an arg to missing.interpolate_1d | ||
return missing.interpolate_1d(index, x, method=method, limit=limit, | ||
limit_direction=limit_direction, | ||
fill_value=fill_value, | ||
bounds_error=False, **kwargs) | ||
|
||
# interp each column independently | ||
interp_values = np.apply_along_axis(func, axis, data) | ||
return interp_values |
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.
huh? where are you testing this
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.
This is a workaround until #19198 is fixed:
(
ser.interpolate
doesn't raise, just forgets to interpolate)