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 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
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
7 changes: 6 additions & 1 deletion pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,14 +463,15 @@ def interpolate_2d(
Perform an actual interpolation of values, values will be make 2-d if
needed fills inplace, returns the result.
"""
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 +491,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
2 changes: 1 addition & 1 deletion pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ def test_resample_dtype_preservation():
assert result.val.dtype == np.int32


def test_resample_dtype_coerceion():
def test_resample_dtype_coercion():

pytest.importorskip("scipy.interpolate")

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,17 @@ def test_interp_datetime64(self, method, tz_naive_fixture):
)
assert_series_equal(result, expected)

def test_interp_pad_datetime64tz_values(self):
# GH#27628 missing.interpolate_2d should handle datetimetz values
dti = pd.date_range("2015-04-05", periods=3, tz="US/Central")
ser = pd.Series(dti)
ser[1] = pd.NaT
result = ser.interpolate(method="pad")

expected = pd.Series(dti)
expected[1] = expected[0]
tm.assert_series_equal(result, expected)

def test_interp_limit_no_nans(self):
# GH 7173
s = pd.Series([1.0, 2.0, 3.0])
Expand Down