From 87ba64014125c07d1305d80bb48303c3f31eee16 Mon Sep 17 00:00:00 2001 From: Tony Lorenzo Date: Tue, 23 Jul 2019 14:25:57 -0700 Subject: [PATCH 1/6] BUG: Fix interpolate ValueError for datetime64_tz index fixes GH27548 --- pandas/core/generic.py | 2 +- pandas/tests/series/test_missing.py | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 0afd42e406c1f..609ab61afb385 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7023,7 +7023,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 8398591f1ff1f..a81023e1213f2 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -1521,12 +1521,23 @@ def test_interp_nonmono_raise(self): s.interpolate(method="krogh") @td.skip_if_no_scipy - def test_interp_datetime64(self): + @pytest.mark.parametrize("method", ["nearest", "pad"]) + def test_interp_datetime64(self, method): df = Series([1, np.nan, 3], index=date_range("1/1/2000", periods=3)) - result = df.interpolate(method="nearest") + result = df.interpolate(method=method) expected = Series([1.0, 1.0, 3.0], index=date_range("1/1/2000", periods=3)) assert_series_equal(result, expected) + @td.skip_if_no_scipy + @pytest.mark.parametrize("method", ["nearest", "pad"]) + def test_interp_datetime64_tz(self, method): + df = Series([1, np.nan, 3], index=date_range("1/1/2000", periods=3, tz="UTC")) + result = df.interpolate(method=method) + expected = Series( + [1.0, 1.0, 3.0], index=date_range("1/1/2000", periods=3, tz="UTC") + ) + assert_series_equal(result, expected) + def test_interp_limit_no_nans(self): # GH 7173 s = pd.Series([1.0, 2.0, 3.0]) From 63269085466a74c11220e4e9dca545b41ffa2f59 Mon Sep 17 00:00:00 2001 From: Tony Lorenzo Date: Tue, 23 Jul 2019 14:32:33 -0700 Subject: [PATCH 2/6] add change to whatsnew --- doc/source/whatsnew/v0.25.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.1.rst b/doc/source/whatsnew/v0.25.1.rst index 69f82f7f85040..e041e25bd98c7 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:`interpolate` when using a timezone aware index (:issue:`27548`) - - From 993feb5770a6969b422f87c29dfb816e5077d300 Mon Sep 17 00:00:00 2001 From: Tony Lorenzo Date: Tue, 23 Jul 2019 15:11:07 -0700 Subject: [PATCH 3/6] remove unused import --- pandas/core/generic.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 609ab61afb385..9053edf2d1424 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, From 41e1311397a11ab16410eb7e311eb5647f759ae6 Mon Sep 17 00:00:00 2001 From: Tony Lorenzo Date: Tue, 23 Jul 2019 15:12:14 -0700 Subject: [PATCH 4/6] fix whatsnew description --- doc/source/whatsnew/v0.25.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.1.rst b/doc/source/whatsnew/v0.25.1.rst index e041e25bd98c7..530a959098591 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:`interpolate` when using a timezone aware index (:issue:`27548`) +- Bug in :meth:`interpolate` when using a timezone aware :class:`DatetimeIndex` (:issue:`27548`) - - From 9c94eb8d6e40db499a64a3d8e82cced6d0186f50 Mon Sep 17 00:00:00 2001 From: Tony Lorenzo Date: Tue, 23 Jul 2019 15:27:09 -0700 Subject: [PATCH 5/6] more whatsnew fix --- doc/source/whatsnew/v0.25.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.1.rst b/doc/source/whatsnew/v0.25.1.rst index 530a959098591..ae2ff33f3f219 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:`interpolate` when using a timezone aware :class:`DatetimeIndex` (:issue:`27548`) +- Bug in :meth:`Series.interpolate` when using a timezone aware :class:`DatetimeIndex` (:issue:`27548`) - - From a04fec672ed3708ae770058f398fccb1baa6a63a Mon Sep 17 00:00:00 2001 From: Tony Lorenzo Date: Tue, 23 Jul 2019 15:35:08 -0700 Subject: [PATCH 6/6] parametrize test with tz_naive_fixture --- pandas/tests/series/test_missing.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index a81023e1213f2..8f4c89ee72ae1 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -1522,19 +1522,14 @@ def test_interp_nonmono_raise(self): @td.skip_if_no_scipy @pytest.mark.parametrize("method", ["nearest", "pad"]) - def test_interp_datetime64(self, method): - df = Series([1, np.nan, 3], index=date_range("1/1/2000", periods=3)) - result = df.interpolate(method=method) - expected = Series([1.0, 1.0, 3.0], index=date_range("1/1/2000", periods=3)) - assert_series_equal(result, expected) - - @td.skip_if_no_scipy - @pytest.mark.parametrize("method", ["nearest", "pad"]) - def test_interp_datetime64_tz(self, method): - df = Series([1, np.nan, 3], index=date_range("1/1/2000", periods=3, tz="UTC")) + 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="UTC") + [1.0, 1.0, 3.0], + index=date_range("1/1/2000", periods=3, tz=tz_naive_fixture), ) assert_series_equal(result, expected)