Skip to content

Commit f0ecfd8

Browse files
authored
API: require timezone match in DatetimeArray.take (#37356)
1 parent c863dde commit f0ecfd8

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ Datetimelike
370370
- Bug in :meth:`DatetimeIndex.slice_locs` where ``datetime.date`` objects were not accepted (:issue:`34077`)
371371
- Bug in :meth:`DatetimeIndex.searchsorted`, :meth:`TimedeltaIndex.searchsorted`, :meth:`PeriodIndex.searchsorted`, and :meth:`Series.searchsorted` with ``datetime64``, ``timedelta64`` or ``Period`` dtype placement of ``NaT`` values being inconsistent with ``NumPy`` (:issue:`36176`, :issue:`36254`)
372372
- Inconsistency in :class:`DatetimeArray`, :class:`TimedeltaArray`, and :class:`PeriodArray` setitem casting arrays of strings to datetimelike scalars but not scalar strings (:issue:`36261`)
373+
- Bug in :meth:`DatetimeArray.take` incorrectly allowing ``fill_value`` with a mismatched timezone (:issue:`37356`)
373374
- Bug in :class:`DatetimeIndex.shift` incorrectly raising when shifting empty indexes (:issue:`14811`)
374375
- :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`)
375376
- Bug in :meth:`DatetimeIndex.equals` and :meth:`TimedeltaIndex.equals` incorrectly considering ``int64`` indexes as equal (:issue:`36744`)

pandas/core/arrays/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ def _validate_fill_value(self, fill_value):
480480
fill_value = self._validate_scalar(fill_value)
481481
except TypeError as err:
482482
raise ValueError(msg) from err
483-
return self._unbox(fill_value)
483+
return self._unbox(fill_value, setitem=True)
484484

485485
def _validate_shift_value(self, fill_value):
486486
# TODO(2.0): once this deprecation is enforced, use _validate_fill_value

pandas/core/arrays/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ def _check_compatible_with(self, other, setitem: bool = False):
473473
if setitem:
474474
# Stricter check for setitem vs comparison methods
475475
if not timezones.tz_compare(self.tz, other.tz):
476-
raise ValueError(f"Timezones don't match. '{self.tz} != {other.tz}'")
476+
raise ValueError(f"Timezones don't match. '{self.tz}' != '{other.tz}'")
477477

478478
def _maybe_clear_freq(self):
479479
self._freq = None

pandas/tests/arrays/test_datetimelike.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55
import pytz
66

7-
from pandas._libs import OutOfBoundsDatetime
7+
from pandas._libs import OutOfBoundsDatetime, Timestamp
88
from pandas.compat.numpy import np_version_under1p18
99

1010
import pandas as pd
@@ -695,6 +695,15 @@ def test_take_fill_valid(self, arr1d):
695695
# require appropriate-dtype if we have a NA value
696696
arr.take([-1, 1], allow_fill=True, fill_value=value)
697697

698+
if arr.tz is not None:
699+
# GH#37356
700+
# Assuming here that arr1d fixture does not include Australia/Melbourne
701+
value = Timestamp.now().tz_localize("Australia/Melbourne")
702+
msg = "Timezones don't match. .* != 'Australia/Melbourne'"
703+
with pytest.raises(ValueError, match=msg):
704+
# require tz match, not just tzawareness match
705+
arr.take([-1, 1], allow_fill=True, fill_value=value)
706+
698707
def test_concat_same_type_invalid(self, arr1d):
699708
# different timezones
700709
arr = arr1d

0 commit comments

Comments
 (0)