From 6e4a75575c1eabef0b6bce98f07585ebd10a7558 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 20 Oct 2020 18:45:41 -0700 Subject: [PATCH 1/3] API: require timezone match in DatetimeArray.shift --- pandas/core/arrays/datetimelike.py | 2 +- pandas/tests/arrays/test_datetimes.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index de0a246861961..cc787fa6b4756 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -508,7 +508,7 @@ def _validate_shift_value(self, fill_value): ) fill_value = new_fill - return self._unbox(fill_value) + return self._unbox(fill_value, setitem=True) def _validate_scalar(self, value, msg: Optional[str] = None): """ diff --git a/pandas/tests/arrays/test_datetimes.py b/pandas/tests/arrays/test_datetimes.py index 78721fc2fe1c1..0559e2eba6019 100644 --- a/pandas/tests/arrays/test_datetimes.py +++ b/pandas/tests/arrays/test_datetimes.py @@ -436,6 +436,18 @@ def test_shift_value_tzawareness_mismatch(self): with pytest.raises(TypeError, match="Cannot compare"): dta.shift(1, fill_value=invalid) + def test_shift_requires_tzmatch(self): + # since filling is setitem-like, we require a matching timezone, + # not just matching tzawawreness + dti = pd.date_range("2016-01-01", periods=3, tz="UTC") + dta = dti._data + + fill_value = pd.Timestamp("2020-10-18 18:44", tz="US/Pacific") + + msg = "Timezones don't match. 'UTC != US/Pacific'" + with pytest.raises(ValueError, match=msg): + dta.shift(1, fill_value=fill_value) + class TestSequenceToDT64NS: def test_tz_dtype_mismatch_raises(self): From 55645456ae3d5cc40cf55bfad5e7b35f0d2b8dfd Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 25 Oct 2020 08:25:53 -0700 Subject: [PATCH 2/3] dummy commit to force CI From 22bbc7a0edb73c95fe69ae32ecf36e7000546967 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 25 Oct 2020 20:50:46 -0700 Subject: [PATCH 3/3] fixup test message --- pandas/tests/arrays/test_datetimes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/arrays/test_datetimes.py b/pandas/tests/arrays/test_datetimes.py index 0559e2eba6019..d6de6534e3191 100644 --- a/pandas/tests/arrays/test_datetimes.py +++ b/pandas/tests/arrays/test_datetimes.py @@ -444,7 +444,7 @@ def test_shift_requires_tzmatch(self): fill_value = pd.Timestamp("2020-10-18 18:44", tz="US/Pacific") - msg = "Timezones don't match. 'UTC != US/Pacific'" + msg = "Timezones don't match. 'UTC' != 'US/Pacific'" with pytest.raises(ValueError, match=msg): dta.shift(1, fill_value=fill_value)