From 1ed927967871f9e2c450c08f90e7f3454ae849ab Mon Sep 17 00:00:00 2001 From: Tony Lorenzo Date: Wed, 24 Jul 2019 04:42:09 -0700 Subject: [PATCH] Backport PR #27549: BUG: Fix interpolate ValueError for datetime64_tz index --- doc/source/whatsnew/v0.25.1.rst | 2 +- pandas/core/generic.py | 3 +-- pandas/tests/series/test_missing.py | 14 ++++++++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v0.25.1.rst b/doc/source/whatsnew/v0.25.1.rst index 6234bc0f7bd35..a769930c9cd9b 100644 --- a/doc/source/whatsnew/v0.25.1.rst +++ b/doc/source/whatsnew/v0.25.1.rst @@ -56,7 +56,7 @@ Timezones Numeric ^^^^^^^ -- +- Bug in :meth:`Series.interpolate` when using a timezone aware :class:`DatetimeIndex` (:issue:`27548`) - - diff --git a/pandas/core/generic.py b/pandas/core/generic.py index f28f58b070368..19f126c36cde7 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -30,7 +30,6 @@ is_bool, is_bool_dtype, is_datetime64_any_dtype, - is_datetime64_dtype, is_datetime64tz_dtype, is_dict_like, is_extension_array_dtype, @@ -7035,7 +7034,7 @@ def interpolate( methods = {"index", "values", "nearest", "time"} is_numeric_or_datetime = ( is_numeric_dtype(index) - or is_datetime64_dtype(index) + or is_datetime64_any_dtype(index) or is_timedelta64_dtype(index) ) if method not in methods and not is_numeric_or_datetime: diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index c5fc52b9b0c41..10375719be8d2 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -1518,10 +1518,16 @@ def test_interp_nonmono_raise(self): s.interpolate(method="krogh") @td.skip_if_no_scipy - def test_interp_datetime64(self): - df = Series([1, np.nan, 3], index=date_range("1/1/2000", periods=3)) - result = df.interpolate(method="nearest") - expected = Series([1.0, 1.0, 3.0], index=date_range("1/1/2000", periods=3)) + @pytest.mark.parametrize("method", ["nearest", "pad"]) + def test_interp_datetime64(self, method, tz_naive_fixture): + df = Series( + [1, np.nan, 3], index=date_range("1/1/2000", periods=3, tz=tz_naive_fixture) + ) + result = df.interpolate(method=method) + expected = Series( + [1.0, 1.0, 3.0], + index=date_range("1/1/2000", periods=3, tz=tz_naive_fixture), + ) assert_series_equal(result, expected) def test_interp_limit_no_nans(self):