Skip to content

Commit dcb6608

Browse files
committed
BUG: cannot subtract Timestamp with different timezones (#31793)
1 parent c03cc22 commit dcb6608

File tree

7 files changed

+112
-35
lines changed

7 files changed

+112
-35
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,7 @@ Timezones
944944
^^^^^^^^^
945945
- Bug in different ``tzinfo`` objects representing UTC not being treated as equivalent (:issue:`39216`)
946946
- Bug in ``dateutil.tz.gettz("UTC")`` not being recognized as equivalent to other UTC-representing tzinfos (:issue:`39276`)
947+
- Bug in ``Timestamp`` and ``DatetimeIndex`` incorrectly raising a ``TypeError`` when subtracting two timezone-aware objects with mismatched timezones (:issue:`31793`)
947948

948949
Numeric
949950
^^^^^^^

pandas/_libs/tslibs/timestamps.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,10 @@ cdef class _Timestamp(ABCTimestamp):
343343
else:
344344
self = type(other)(self)
345345

346-
# validate tz's
347-
if not tz_compare(self.tzinfo, other.tzinfo):
348-
raise TypeError("Timestamp subtraction must have the "
349-
"same timezones or no timezones")
346+
if (self.tzinfo is None) ^ (other.tzinfo is None):
347+
raise TypeError(
348+
"Cannot subtract tz-naive and tz-aware datetime-like objects."
349+
)
350350

351351
# scalar Timestamp/datetime - Timestamp/datetime -> yields a
352352
# Timedelta

pandas/core/arrays/datetimes.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -719,12 +719,11 @@ def _sub_datetime_arraylike(self, other):
719719
assert is_datetime64_dtype(other)
720720
other = type(self)(other)
721721

722-
if not self._has_same_tz(other):
723-
# require tz compat
724-
raise TypeError(
725-
f"{type(self).__name__} subtraction must have the same "
726-
"timezones or no timezones"
727-
)
722+
try:
723+
self._assert_tzawareness_compat(other)
724+
except TypeError as error:
725+
new_message = str(error).replace("compare", "subtract")
726+
raise type(error)(new_message) from error
728727

729728
self_i8 = self.asi8
730729
other_i8 = other.asi8
@@ -770,11 +769,11 @@ def _sub_datetimelike_scalar(self, other):
770769
if other is NaT: # type: ignore[comparison-overlap]
771770
return self - NaT
772771

773-
if not self._has_same_tz(other):
774-
# require tz compat
775-
raise TypeError(
776-
"Timestamp subtraction must have the same timezones or no timezones"
777-
)
772+
try:
773+
self._assert_tzawareness_compat(other)
774+
except TypeError as error:
775+
new_message = str(error).replace("compare", "subtract")
776+
raise type(error)(new_message) from error
778777

779778
i8 = self.asi8
780779
result = checked_add_with_arr(i8, -other.value, arr_mask=self._isnan)

pandas/tests/arithmetic/test_datetime64.py

+57-6
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,61 @@ def test_dt64arr_isub_timedeltalike_scalar(
864864
rng -= two_hours
865865
tm.assert_equal(rng, expected)
866866

867+
def test_dt64_array_sub_dt_with_different_timezone(self, box_with_array):
868+
t1 = date_range("20130101", periods=3).tz_localize("US/Eastern")
869+
t1 = tm.box_expected(t1, box_with_array)
870+
t2 = Timestamp("20130101").tz_localize("CET")
871+
tnaive = Timestamp(20130101)
872+
873+
result = t1 - t2
874+
expected = TimedeltaIndex(
875+
["0 days 06:00:00", "1 days 06:00:00", "2 days 06:00:00"]
876+
)
877+
expected = tm.box_expected(expected, box_with_array)
878+
tm.assert_equal(result, expected)
879+
880+
result = t2 - t1
881+
expected = TimedeltaIndex(
882+
["-1 days +18:00:00", "-2 days +18:00:00", "-3 days +18:00:00"]
883+
)
884+
expected = tm.box_expected(expected, box_with_array)
885+
tm.assert_equal(result, expected)
886+
887+
msg = "Cannot subtract tz-naive and tz-aware datetime-like objects"
888+
with pytest.raises(TypeError, match=msg):
889+
t1 - tnaive
890+
891+
with pytest.raises(TypeError, match=msg):
892+
tnaive - t1
893+
894+
def test_dt64_array_sub_dt64_array_with_different_timezone(self, box_with_array):
895+
t1 = date_range("20130101", periods=3).tz_localize("US/Eastern")
896+
t1 = tm.box_expected(t1, box_with_array)
897+
t2 = date_range("20130101", periods=3).tz_localize("CET")
898+
t2 = tm.box_expected(t2, box_with_array)
899+
tnaive = date_range("20130101", periods=3)
900+
901+
result = t1 - t2
902+
expected = TimedeltaIndex(
903+
["0 days 06:00:00", "0 days 06:00:00", "0 days 06:00:00"]
904+
)
905+
expected = tm.box_expected(expected, box_with_array)
906+
tm.assert_equal(result, expected)
907+
908+
result = t2 - t1
909+
expected = TimedeltaIndex(
910+
["-1 days +18:00:00", "-1 days +18:00:00", "-1 days +18:00:00"]
911+
)
912+
expected = tm.box_expected(expected, box_with_array)
913+
tm.assert_equal(result, expected)
914+
915+
msg = "Cannot subtract tz-naive and tz-aware datetime-like objects"
916+
with pytest.raises(TypeError, match=msg):
917+
t1 - tnaive
918+
919+
with pytest.raises(TypeError, match=msg):
920+
tnaive - t1
921+
867922
# TODO: redundant with test_dt64arr_add_timedeltalike_scalar
868923
def test_dt64arr_add_td64_scalar(self, box_with_array):
869924
# scalar timedeltas/np.timedelta64 objects
@@ -1045,7 +1100,7 @@ def test_dt64arr_aware_sub_dt64ndarray_raises(
10451100
dt64vals = dti.values
10461101

10471102
dtarr = tm.box_expected(dti, box_with_array)
1048-
msg = "subtraction must have the same timezones or"
1103+
msg = "Cannot subtract tz-naive and tz-aware datetime"
10491104
with pytest.raises(TypeError, match=msg):
10501105
dtarr - dt64vals
10511106
with pytest.raises(TypeError, match=msg):
@@ -2229,24 +2284,20 @@ def test_sub_dti_dti(self):
22292284

22302285
dti = date_range("20130101", periods=3)
22312286
dti_tz = date_range("20130101", periods=3).tz_localize("US/Eastern")
2232-
dti_tz2 = date_range("20130101", periods=3).tz_localize("UTC")
22332287
expected = TimedeltaIndex([0, 0, 0])
22342288

22352289
result = dti - dti
22362290
tm.assert_index_equal(result, expected)
22372291

22382292
result = dti_tz - dti_tz
22392293
tm.assert_index_equal(result, expected)
2240-
msg = "DatetimeArray subtraction must have the same timezones or"
2294+
msg = "Cannot subtract tz-naive and tz-aware datetime-like objects"
22412295
with pytest.raises(TypeError, match=msg):
22422296
dti_tz - dti
22432297

22442298
with pytest.raises(TypeError, match=msg):
22452299
dti - dti_tz
22462300

2247-
with pytest.raises(TypeError, match=msg):
2248-
dti_tz - dti_tz2
2249-
22502301
# isub
22512302
dti -= dti
22522303
tm.assert_index_equal(dti, expected)

pandas/tests/arithmetic/test_timedelta64.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -390,35 +390,29 @@ def _check(result, expected):
390390
_check(result, expected)
391391

392392
# tz mismatches
393-
msg = "Timestamp subtraction must have the same timezones or no timezones"
393+
msg = "Cannot subtract tz-naive and tz-aware datetime-like objects."
394394
with pytest.raises(TypeError, match=msg):
395395
dt_tz - ts
396396
msg = "can't subtract offset-naive and offset-aware datetimes"
397397
with pytest.raises(TypeError, match=msg):
398398
dt_tz - dt
399-
msg = "Timestamp subtraction must have the same timezones or no timezones"
400-
with pytest.raises(TypeError, match=msg):
401-
dt_tz - ts_tz2
402399
msg = "can't subtract offset-naive and offset-aware datetimes"
403400
with pytest.raises(TypeError, match=msg):
404401
dt - dt_tz
405-
msg = "Timestamp subtraction must have the same timezones or no timezones"
402+
msg = "Cannot subtract tz-naive and tz-aware datetime-like objects."
406403
with pytest.raises(TypeError, match=msg):
407404
ts - dt_tz
408405
with pytest.raises(TypeError, match=msg):
409406
ts_tz2 - ts
410407
with pytest.raises(TypeError, match=msg):
411408
ts_tz2 - dt
412-
with pytest.raises(TypeError, match=msg):
413-
ts_tz - ts_tz2
414409

410+
msg = "Cannot subtract tz-naive and tz-aware"
415411
# with dti
416412
with pytest.raises(TypeError, match=msg):
417413
dti - ts_tz
418414
with pytest.raises(TypeError, match=msg):
419415
dti_tz - ts
420-
with pytest.raises(TypeError, match=msg):
421-
dti_tz - ts_tz2
422416

423417
result = dti_tz - dt_tz
424418
expected = TimedeltaIndex(["0 days", "1 days", "2 days"])

pandas/tests/scalar/timestamp/test_arithmetic.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import (
22
datetime,
33
timedelta,
4+
timezone,
45
)
56

67
import numpy as np
@@ -99,7 +100,7 @@ def test_rsub_dtscalars(self, tz_naive_fixture):
99100
if tz_naive_fixture is None:
100101
assert other.to_datetime64() - ts == td
101102
else:
102-
msg = "subtraction must have"
103+
msg = "Cannot subtract tz-naive and tz-aware datetime-like objects"
103104
with pytest.raises(TypeError, match=msg):
104105
other.to_datetime64() - ts
105106

@@ -109,6 +110,37 @@ def test_timestamp_sub_datetime(self):
109110
assert (ts - dt).days == 1
110111
assert (dt - ts).days == -1
111112

113+
def test_subtract_tzaware_datetime(self):
114+
t1 = Timestamp("2020-10-22T22:00:00+00:00")
115+
t2 = datetime(2020, 10, 22, 22, tzinfo=timezone.utc)
116+
117+
result = t1 - t2
118+
119+
assert isinstance(result, Timedelta)
120+
assert result == Timedelta("0 days")
121+
122+
def test_subtract_timestamp_from_different_timezone(self):
123+
t1 = Timestamp("20130101").tz_localize("US/Eastern")
124+
t2 = Timestamp("20130101").tz_localize("CET")
125+
126+
result = t1 - t2
127+
128+
assert isinstance(result, Timedelta)
129+
assert result == Timedelta("0 days 06:00:00")
130+
131+
def test_subtracting_involving_datetime_with_different_tz(self):
132+
t1 = datetime(2013, 1, 1, tzinfo=timezone(timedelta(hours=-5)))
133+
t2 = Timestamp("20130101").tz_localize("CET")
134+
135+
result = t1 - t2
136+
137+
assert isinstance(result, Timedelta)
138+
assert result == Timedelta("0 days 06:00:00")
139+
140+
result = t2 - t1
141+
assert isinstance(result, Timedelta)
142+
assert result == Timedelta("-1 days +18:00:00")
143+
112144
def test_addition_subtraction_types(self):
113145
# Assert on the types resulting from Timestamp +/- various date/time
114146
# objects

pandas/tests/series/methods/test_shift.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ def test_shift(self, datetime_series):
110110
exp = Series(TimedeltaIndex(["NaT"] + ["1 days"] * 4), name="foo")
111111
tm.assert_series_equal(result, exp)
112112

113-
# incompat tz
113+
# mismatched timezone
114114
s2 = Series(date_range("2000-01-01 09:00:00", periods=5, tz="CET"), name="foo")
115-
msg = "DatetimeArray subtraction must have the same timezones or no timezones"
116-
with pytest.raises(TypeError, match=msg):
117-
s - s2
115+
exp = Series(TimedeltaIndex(["6 hours"] * 5), name="foo")
116+
result = s - s2
117+
tm.assert_series_equal(result, exp)
118118

119119
def test_shift2(self):
120120
ts = Series(

0 commit comments

Comments
 (0)