Skip to content

Commit 214fe05

Browse files
jbrockmendelquintusdias
authored andcommitted
Make interpolate_2d handle datetime64 correctly (pandas-dev#27628)
1 parent dfb8e3f commit 214fe05

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

pandas/core/internals/blocks.py

-8
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,6 @@ def _interpolate_with_fill(
12201220
fill_value=fill_value,
12211221
dtype=self.dtype,
12221222
)
1223-
values = self._try_coerce_result(values)
12241223

12251224
blocks = [self.make_block_same_class(values, ndim=self.ndim)]
12261225
return self._maybe_downcast(blocks, downcast)
@@ -2278,13 +2277,6 @@ def _try_coerce_args(self, other):
22782277

22792278
return other
22802279

2281-
def _try_coerce_result(self, result):
2282-
""" reverse of try_coerce_args """
2283-
if isinstance(result, np.ndarray) and result.dtype.kind == "i":
2284-
# needed for _interpolate_with_ffill
2285-
result = result.view("M8[ns]")
2286-
return result
2287-
22882280
def to_native_types(
22892281
self, slicer=None, na_rep=None, date_format=None, quoting=None, **kwargs
22902282
):

pandas/core/missing.py

+5
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ def interpolate_2d(
463463
Perform an actual interpolation of values, values will be make 2-d if
464464
needed fills inplace, returns the result.
465465
"""
466+
orig_values = values
466467

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

@@ -490,6 +491,10 @@ def interpolate_2d(
490491
if ndim == 1:
491492
values = values[0]
492493

494+
if orig_values.dtype.kind == "M":
495+
# convert float back to datetime64
496+
values = values.astype(orig_values.dtype)
497+
493498
return values
494499

495500

pandas/tests/resample/test_datetime_index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ def test_resample_dtype_preservation():
885885
assert result.val.dtype == np.int32
886886

887887

888-
def test_resample_dtype_coerceion():
888+
def test_resample_dtype_coercion():
889889

890890
pytest.importorskip("scipy.interpolate")
891891

pandas/tests/series/test_missing.py

+11
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,17 @@ def test_interp_datetime64(self, method, tz_naive_fixture):
15331533
)
15341534
assert_series_equal(result, expected)
15351535

1536+
def test_interp_pad_datetime64tz_values(self):
1537+
# GH#27628 missing.interpolate_2d should handle datetimetz values
1538+
dti = pd.date_range("2015-04-05", periods=3, tz="US/Central")
1539+
ser = pd.Series(dti)
1540+
ser[1] = pd.NaT
1541+
result = ser.interpolate(method="pad")
1542+
1543+
expected = pd.Series(dti)
1544+
expected[1] = expected[0]
1545+
tm.assert_series_equal(result, expected)
1546+
15361547
def test_interp_limit_no_nans(self):
15371548
# GH 7173
15381549
s = pd.Series([1.0, 2.0, 3.0])

0 commit comments

Comments
 (0)