@@ -837,6 +837,61 @@ def test_dt64arr_sub_timedeltalike_scalar(
837
837
rng -= two_hours
838
838
tm .assert_equal (rng , expected )
839
839
840
+ def test_dt64_array_sub_dt_with_different_timezone (self , box_with_array ):
841
+ t1 = date_range ("20130101" , periods = 3 ).tz_localize ("US/Eastern" )
842
+ t1 = tm .box_expected (t1 , box_with_array )
843
+ t2 = Timestamp ("20130101" ).tz_localize ("CET" )
844
+ tnaive = Timestamp (20130101 )
845
+
846
+ result = t1 - t2
847
+ expected = TimedeltaIndex (
848
+ ["0 days 06:00:00" , "1 days 06:00:00" , "2 days 06:00:00" ]
849
+ )
850
+ expected = tm .box_expected (expected , box_with_array )
851
+ tm .assert_equal (result , expected )
852
+
853
+ result = t2 - t1
854
+ expected = TimedeltaIndex (
855
+ ["-1 days +18:00:00" , "-2 days +18:00:00" , "-3 days +18:00:00" ]
856
+ )
857
+ expected = tm .box_expected (expected , box_with_array )
858
+ tm .assert_equal (result , expected )
859
+
860
+ msg = "Cannot subtract tz-naive and tz-aware datetime-like objects"
861
+ with pytest .raises (TypeError , match = msg ):
862
+ t1 - tnaive
863
+
864
+ with pytest .raises (TypeError , match = msg ):
865
+ tnaive - t1
866
+
867
+ def test_dt64_array_sub_dt64_array_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 = date_range ("20130101" , periods = 3 ).tz_localize ("CET" )
871
+ t2 = tm .box_expected (t2 , box_with_array )
872
+ tnaive = date_range ("20130101" , periods = 3 )
873
+
874
+ result = t1 - t2
875
+ expected = TimedeltaIndex (
876
+ ["0 days 06:00:00" , "0 days 06:00:00" , "0 days 06:00:00" ]
877
+ )
878
+ expected = tm .box_expected (expected , box_with_array )
879
+ tm .assert_equal (result , expected )
880
+
881
+ result = t2 - t1
882
+ expected = TimedeltaIndex (
883
+ ["-1 days +18:00:00" , "-1 days +18:00:00" , "-1 days +18:00:00" ]
884
+ )
885
+ expected = tm .box_expected (expected , box_with_array )
886
+ tm .assert_equal (result , expected )
887
+
888
+ msg = "Cannot subtract tz-naive and tz-aware datetime-like objects"
889
+ with pytest .raises (TypeError , match = msg ):
890
+ t1 - tnaive
891
+
892
+ with pytest .raises (TypeError , match = msg ):
893
+ tnaive - t1
894
+
840
895
def test_dt64arr_add_sub_td64_nat (self , box_with_array , tz_naive_fixture ):
841
896
# GH#23320 special handling for timedelta64("NaT")
842
897
tz = tz_naive_fixture
@@ -979,7 +1034,7 @@ def test_dt64arr_aware_sub_dt64ndarray_raises(
979
1034
dt64vals = dti .values
980
1035
981
1036
dtarr = tm .box_expected (dti , box_with_array )
982
- msg = "subtraction must have the same timezones or "
1037
+ msg = "Cannot subtract tz-naive and tz-aware datetime "
983
1038
with pytest .raises (TypeError , match = msg ):
984
1039
dtarr - dt64vals
985
1040
with pytest .raises (TypeError , match = msg ):
@@ -2099,24 +2154,20 @@ def test_sub_dti_dti(self):
2099
2154
2100
2155
dti = date_range ("20130101" , periods = 3 )
2101
2156
dti_tz = date_range ("20130101" , periods = 3 ).tz_localize ("US/Eastern" )
2102
- dti_tz2 = date_range ("20130101" , periods = 3 ).tz_localize ("UTC" )
2103
2157
expected = TimedeltaIndex ([0 , 0 , 0 ])
2104
2158
2105
2159
result = dti - dti
2106
2160
tm .assert_index_equal (result , expected )
2107
2161
2108
2162
result = dti_tz - dti_tz
2109
2163
tm .assert_index_equal (result , expected )
2110
- msg = "DatetimeArray subtraction must have the same timezones or "
2164
+ msg = "Cannot subtract tz-naive and tz-aware datetime-like objects "
2111
2165
with pytest .raises (TypeError , match = msg ):
2112
2166
dti_tz - dti
2113
2167
2114
2168
with pytest .raises (TypeError , match = msg ):
2115
2169
dti - dti_tz
2116
2170
2117
- with pytest .raises (TypeError , match = msg ):
2118
- dti_tz - dti_tz2
2119
-
2120
2171
# isub
2121
2172
dti -= dti
2122
2173
tm .assert_index_equal (dti , expected )
0 commit comments