Skip to content

Commit 6444c29

Browse files
jbrockmendelukarroum
authored andcommitted
API: require timezone match in DatetimeArray.shift (pandas-dev#37299)
1 parent 1b9cb42 commit 6444c29

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ Datetimelike
389389
- :class:`Timestamp` and :class:`DatetimeIndex` comparisons between timezone-aware and timezone-naive objects now follow the standard library ``datetime`` behavior, returning ``True``/``False`` for ``!=``/``==`` and raising for inequality comparisons (:issue:`28507`)
390390
- Bug in :meth:`DatetimeIndex.equals` and :meth:`TimedeltaIndex.equals` incorrectly considering ``int64`` indexes as equal (:issue:`36744`)
391391
- Bug in :meth:`TimedeltaIndex.sum` and :meth:`Series.sum` with ``timedelta64`` dtype on an empty index or series returning ``NaT`` instead of ``Timedelta(0)`` (:issue:`31751`)
392+
- Bug in :meth:`DatetimeArray.shift` incorrectly allowing ``fill_value`` with a mismatched timezone (:issue:`37299`)
392393

393394
Timedelta
394395
^^^^^^^^^

pandas/core/arrays/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ def _validate_shift_value(self, fill_value):
507507
)
508508
fill_value = new_fill
509509

510-
return self._unbox(fill_value)
510+
return self._unbox(fill_value, setitem=True)
511511

512512
def _validate_scalar(self, value, allow_listlike: bool = False):
513513
"""

pandas/tests/arrays/test_datetimes.py

+12
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,18 @@ def test_shift_value_tzawareness_mismatch(self):
437437
with pytest.raises(TypeError, match="Cannot compare"):
438438
dta.shift(1, fill_value=invalid)
439439

440+
def test_shift_requires_tzmatch(self):
441+
# since filling is setitem-like, we require a matching timezone,
442+
# not just matching tzawawreness
443+
dti = pd.date_range("2016-01-01", periods=3, tz="UTC")
444+
dta = dti._data
445+
446+
fill_value = pd.Timestamp("2020-10-18 18:44", tz="US/Pacific")
447+
448+
msg = "Timezones don't match. 'UTC' != 'US/Pacific'"
449+
with pytest.raises(ValueError, match=msg):
450+
dta.shift(1, fill_value=fill_value)
451+
440452

441453
class TestSequenceToDT64NS:
442454
def test_tz_dtype_mismatch_raises(self):

0 commit comments

Comments
 (0)