Skip to content

Commit bebaff0

Browse files
jbrockmendeljreback
authored andcommitted
Make DTA _check_compatible_with less strict by default (#30721)
1 parent 04c3d51 commit bebaff0

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

pandas/core/arrays/datetimelike.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def _unbox_scalar(self, value: Union[Period, Timestamp, Timedelta, NaTType]) ->
109109
raise AbstractMethodError(self)
110110

111111
def _check_compatible_with(
112-
self, other: Union[Period, Timestamp, Timedelta, NaTType]
112+
self, other: Union[Period, Timestamp, Timedelta, NaTType], setitem: bool = False
113113
) -> None:
114114
"""
115115
Verify that `self` and `other` are compatible.
@@ -123,6 +123,9 @@ def _check_compatible_with(
123123
Parameters
124124
----------
125125
other
126+
setitem : bool, default False
127+
For __setitem__ we may have stricter compatiblity resrictions than
128+
for comparisons.
126129
127130
Raises
128131
------
@@ -500,10 +503,10 @@ def __setitem__(
500503
return
501504

502505
value = type(self)._from_sequence(value, dtype=self.dtype)
503-
self._check_compatible_with(value)
506+
self._check_compatible_with(value, setitem=True)
504507
value = value.asi8
505508
elif isinstance(value, self._scalar_type):
506-
self._check_compatible_with(value)
509+
self._check_compatible_with(value, setitem=True)
507510
value = self._unbox_scalar(value)
508511
elif is_valid_nat_for_dtype(value, self.dtype):
509512
value = iNaT

pandas/core/arrays/datetimes.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -553,11 +553,14 @@ def _unbox_scalar(self, value):
553553
def _scalar_from_string(self, value):
554554
return Timestamp(value, tz=self.tz)
555555

556-
def _check_compatible_with(self, other):
556+
def _check_compatible_with(self, other, setitem: bool = False):
557557
if other is NaT:
558558
return
559-
if not timezones.tz_compare(self.tz, other.tz):
560-
raise ValueError(f"Timezones don't match. '{self.tz} != {other.tz}'")
559+
self._assert_tzawareness_compat(other)
560+
if setitem:
561+
# Stricter check for setitem vs comparison methods
562+
if not timezones.tz_compare(self.tz, other.tz):
563+
raise ValueError(f"Timezones don't match. '{self.tz} != {other.tz}'")
561564

562565
def _maybe_clear_freq(self):
563566
self._freq = None

pandas/core/arrays/period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def _unbox_scalar(self, value: Union[Period, NaTType]) -> int:
341341
def _scalar_from_string(self, value: str) -> Period:
342342
return Period(value, freq=self.freq)
343343

344-
def _check_compatible_with(self, other):
344+
def _check_compatible_with(self, other, setitem: bool = False):
345345
if other is NaT:
346346
return
347347
if self.freqstr != other.freqstr:

pandas/core/arrays/timedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def _unbox_scalar(self, value):
357357
def _scalar_from_string(self, value):
358358
return Timedelta(value)
359359

360-
def _check_compatible_with(self, other):
360+
def _check_compatible_with(self, other, setitem: bool = False):
361361
# we don't have anything to validate.
362362
pass
363363

pandas/tests/arrays/test_datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def test_tz_setter_raises(self):
173173
def test_setitem_different_tz_raises(self):
174174
data = np.array([1, 2, 3], dtype="M8[ns]")
175175
arr = DatetimeArray(data, copy=False, dtype=DatetimeTZDtype(tz="US/Central"))
176-
with pytest.raises(ValueError, match="None"):
176+
with pytest.raises(TypeError, match="Cannot compare tz-naive and tz-aware"):
177177
arr[0] = pd.Timestamp("2000")
178178

179179
with pytest.raises(ValueError, match="US/Central"):

0 commit comments

Comments
 (0)