Skip to content

Make interpolate_2d handle datetime64 correctly #27628

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

Merged
merged 4 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 0 additions & 8 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,6 @@ def _interpolate_with_fill(
fill_value=fill_value,
dtype=self.dtype,
)
values = self._try_coerce_result(values)

blocks = [self.make_block_same_class(values, ndim=self.ndim)]
return self._maybe_downcast(blocks, downcast)
Expand Down Expand Up @@ -2293,13 +2292,6 @@ def _try_coerce_args(self, other):

return other

def _try_coerce_result(self, result):
""" reverse of try_coerce_args """
if isinstance(result, np.ndarray) and result.dtype.kind == "i":
# needed for _interpolate_with_ffill
result = result.view("M8[ns]")
return result

def to_native_types(
self, slicer=None, na_rep=None, date_format=None, quoting=None, **kwargs
):
Expand Down
19 changes: 18 additions & 1 deletion pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,14 +463,27 @@ def interpolate_2d(
Perform an actual interpolation of values, values will be make 2-d if
needed fills inplace, returns the result.
"""
if is_datetime64tz_dtype(values):
naive = values.view("M8[ns]")
result = interpolate_2d(
naive,
method=method,
axis=axis,
limit=limit,
fill_value=fill_value,
dtype=dtype,
)
return type(values)._from_sequence(result, dtype=values.dtype)

orig_values = values

transf = (lambda x: x) if axis == 0 else (lambda x: x.T)

# reshape a 1 dim if needed
ndim = values.ndim
if values.ndim == 1:
if axis != 0: # pragma: no cover
raise AssertionError("cannot interpolate on a ndim == 1 with " "axis != 0")
raise AssertionError("cannot interpolate on a ndim == 1 with axis != 0")
values = values.reshape(tuple((1,) + values.shape))

if fill_value is None:
Expand All @@ -490,6 +503,10 @@ def interpolate_2d(
if ndim == 1:
values = values[0]

if orig_values.dtype.kind == "M":
# convert float back to datetime64
values = values.astype(orig_values.dtype)

return values


Expand Down