2929import pandas .core .arrays .datetimelike as dtl
3030from pandas .core .indexes .datetimes import _to_M8
3131from pandas .core .ops import roperator
32+ from pandas .tests .arithmetic .common import (
33+ assert_invalid_addsub_type ,
34+ assert_invalid_comparison ,
35+ get_upcast_box ,
36+ )
3237import pandas .util .testing as tm
3338
34-
35- def assert_invalid_comparison (left , right , box ):
36- """
37- Assert that comparison operations with mismatched types behave correctly.
38-
39- Parameters
40- ----------
41- left : np.ndarray, ExtensionArray, Index, or Series
42- right : object
43- box : {pd.DataFrame, pd.Series, pd.Index, tm.to_array}
44- """
45- # Not for tznaive-tzaware comparison
46-
47- # Note: not quite the same as how we do this for tm.box_expected
48- xbox = box if box is not pd .Index else np .array
49-
50- result = left == right
51- expected = xbox (np .zeros (result .shape , dtype = np .bool_ ))
52-
53- tm .assert_equal (result , expected )
54-
55- result = right == left
56- tm .assert_equal (result , expected )
57-
58- result = left != right
59- tm .assert_equal (result , ~ expected )
60-
61- result = right != left
62- tm .assert_equal (result , ~ expected )
63-
64- msg = "Invalid comparison between"
65- with pytest .raises (TypeError , match = msg ):
66- left < right
67- with pytest .raises (TypeError , match = msg ):
68- left <= right
69- with pytest .raises (TypeError , match = msg ):
70- left > right
71- with pytest .raises (TypeError , match = msg ):
72- left >= right
73- with pytest .raises (TypeError , match = msg ):
74- right < left
75- with pytest .raises (TypeError , match = msg ):
76- right <= left
77- with pytest .raises (TypeError , match = msg ):
78- right > left
79- with pytest .raises (TypeError , match = msg ):
80- right >= left
81-
82-
8339# ------------------------------------------------------------------
8440# Comparisons
8541
@@ -1033,14 +989,7 @@ def test_dt64arr_add_sub_invalid(self, dti_freq, other, box_with_array):
1033989 "ufunc '?(add|subtract)'? cannot use operands with types" ,
1034990 ]
1035991 )
1036- with pytest .raises (TypeError , match = msg ):
1037- dtarr + other
1038- with pytest .raises (TypeError , match = msg ):
1039- other + dtarr
1040- with pytest .raises (TypeError , match = msg ):
1041- dtarr - other
1042- with pytest .raises (TypeError , match = msg ):
1043- other - dtarr
992+ assert_invalid_addsub_type (dtarr , other , msg )
1044993
1045994 @pytest .mark .parametrize ("pi_freq" , ["D" , "W" , "Q" , "H" ])
1046995 @pytest .mark .parametrize ("dti_freq" , [None , "D" ])
@@ -1061,14 +1010,7 @@ def test_dt64arr_add_sub_parr(
10611010 "ufunc.*cannot use operands" ,
10621011 ]
10631012 )
1064- with pytest .raises (TypeError , match = msg ):
1065- dtarr + parr
1066- with pytest .raises (TypeError , match = msg ):
1067- parr + dtarr
1068- with pytest .raises (TypeError , match = msg ):
1069- dtarr - parr
1070- with pytest .raises (TypeError , match = msg ):
1071- parr - dtarr
1013+ assert_invalid_addsub_type (dtarr , parr , msg )
10721014
10731015
10741016class TestDatetime64DateOffsetArithmetic :
@@ -2368,7 +2310,6 @@ def test_dti_addsub_offset_arraylike(
23682310 # GH#18849, GH#19744
23692311 box = pd .Index
23702312 other_box = index_or_series
2371- from .test_timedelta64 import get_upcast_box
23722313
23732314 tz = tz_naive_fixture
23742315 dti = pd .date_range ("2017-01-01" , periods = 2 , tz = tz , name = names [0 ])
0 commit comments