29
29
import pandas .core .arrays .datetimelike as dtl
30
30
from pandas .core .indexes .datetimes import _to_M8
31
31
from 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
+ )
32
37
import pandas .util .testing as tm
33
38
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
-
83
39
# ------------------------------------------------------------------
84
40
# Comparisons
85
41
@@ -1033,14 +989,7 @@ def test_dt64arr_add_sub_invalid(self, dti_freq, other, box_with_array):
1033
989
"ufunc '?(add|subtract)'? cannot use operands with types" ,
1034
990
]
1035
991
)
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 )
1044
993
1045
994
@pytest .mark .parametrize ("pi_freq" , ["D" , "W" , "Q" , "H" ])
1046
995
@pytest .mark .parametrize ("dti_freq" , [None , "D" ])
@@ -1061,14 +1010,7 @@ def test_dt64arr_add_sub_parr(
1061
1010
"ufunc.*cannot use operands" ,
1062
1011
]
1063
1012
)
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 )
1072
1014
1073
1015
1074
1016
class TestDatetime64DateOffsetArithmetic :
@@ -2368,7 +2310,6 @@ def test_dti_addsub_offset_arraylike(
2368
2310
# GH#18849, GH#19744
2369
2311
box = pd .Index
2370
2312
other_box = index_or_series
2371
- from .test_timedelta64 import get_upcast_box
2372
2313
2373
2314
tz = tz_naive_fixture
2374
2315
dti = pd .date_range ("2017-01-01" , periods = 2 , tz = tz , name = names [0 ])
0 commit comments