From 148d41e2253bb70598766b448a9a7c26dda60f26 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 27 Jul 2019 18:10:49 -0700 Subject: [PATCH 1/4] ffill part of 27626 --- pandas/core/internals/blocks.py | 7 ------- pandas/core/missing.py | 19 ++++++++++++++++++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 4ca867b1088e7..966c3644462c2 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -2293,13 +2293,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 ): diff --git a/pandas/core/missing.py b/pandas/core/missing.py index 8f0abc91f7aef..6318bfcb83dd5 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -463,6 +463,19 @@ 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) @@ -470,7 +483,7 @@ def interpolate_2d( 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: @@ -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 From 6e6af822346e2a89ae7a91a9ec2664f4fc4c56b9 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 27 Jul 2019 18:12:42 -0700 Subject: [PATCH 2/4] remove unnecessary coerce --- pandas/core/internals/blocks.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 966c3644462c2..98397e81d7bea 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -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) From 8ca5aa572fc1ba05051de084aaeef8e158ab11b5 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 28 Jul 2019 09:08:17 -0700 Subject: [PATCH 3/4] remove unnecessary, add test for dtz values --- pandas/core/missing.py | 12 ------------ pandas/tests/resample/test_datetime_index.py | 2 +- pandas/tests/series/test_missing.py | 11 +++++++++++ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/pandas/core/missing.py b/pandas/core/missing.py index 6318bfcb83dd5..19e4c3166da71 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -463,18 +463,6 @@ 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) diff --git a/pandas/tests/resample/test_datetime_index.py b/pandas/tests/resample/test_datetime_index.py index 929bd1725b30a..fb3d428bcf4bf 100644 --- a/pandas/tests/resample/test_datetime_index.py +++ b/pandas/tests/resample/test_datetime_index.py @@ -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") diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index 8f4c89ee72ae1..95017285cfb58 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -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]) From 302d934ae9d57d717d15fe71eaa4eb7fc6bc9c83 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 28 Jul 2019 11:03:08 -0700 Subject: [PATCH 4/4] blackify --- pandas/tests/series/test_missing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index 95017285cfb58..f1b84acf68755 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -1535,7 +1535,7 @@ def test_interp_datetime64(self, method, tz_naive_fixture): 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") + 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")