41
41
)
42
42
from pandas .core .ops import roperator
43
43
from pandas .tests .arithmetic .common import (
44
+ assert_cannot_add ,
44
45
assert_invalid_addsub_type ,
45
46
assert_invalid_comparison ,
46
47
get_upcast_box ,
@@ -99,6 +100,7 @@ def test_dt64arr_cmp_scalar_invalid(self, other, tz_naive_fixture, box_with_arra
99
100
@pytest .mark .parametrize (
100
101
"other" ,
101
102
[
103
+ # GH#4968 invalid date/int comparisons
102
104
list (range (10 )),
103
105
np .arange (10 ),
104
106
np .arange (10 ).astype (np .float32 ),
@@ -111,13 +113,14 @@ def test_dt64arr_cmp_scalar_invalid(self, other, tz_naive_fixture, box_with_arra
111
113
pd .period_range ("1971-01-01" , freq = "D" , periods = 10 ).astype (object ),
112
114
],
113
115
)
114
- def test_dt64arr_cmp_arraylike_invalid (self , other , tz_naive_fixture ):
115
- # We don't parametrize this over box_with_array because listlike
116
- # other plays poorly with assert_invalid_comparison reversed checks
116
+ def test_dt64arr_cmp_arraylike_invalid (
117
+ self , other , tz_naive_fixture , box_with_array
118
+ ):
117
119
tz = tz_naive_fixture
118
120
119
121
dta = date_range ("1970-01-01" , freq = "ns" , periods = 10 , tz = tz )._data
120
- assert_invalid_comparison (dta , other , tm .to_array )
122
+ obj = tm .box_expected (dta , box_with_array )
123
+ assert_invalid_comparison (obj , other , box_with_array )
121
124
122
125
def test_dt64arr_cmp_mixed_invalid (self , tz_naive_fixture ):
123
126
tz = tz_naive_fixture
@@ -215,18 +218,6 @@ def test_nat_comparisons(
215
218
216
219
tm .assert_series_equal (result , expected )
217
220
218
- def test_comparison_invalid (self , tz_naive_fixture , box_with_array ):
219
- # GH#4968
220
- # invalid date/int comparisons
221
- tz = tz_naive_fixture
222
- ser = Series (range (5 ))
223
- ser2 = Series (date_range ("20010101" , periods = 5 , tz = tz ))
224
-
225
- ser = tm .box_expected (ser , box_with_array )
226
- ser2 = tm .box_expected (ser2 , box_with_array )
227
-
228
- assert_invalid_comparison (ser , ser2 , box_with_array )
229
-
230
221
@pytest .mark .parametrize (
231
222
"data" ,
232
223
[
@@ -315,8 +306,8 @@ def test_timestamp_compare_series(self, left, right):
315
306
tm .assert_series_equal (result , expected )
316
307
317
308
# Compare to NaT with series containing NaT
318
- expected = left_f (s_nat , Timestamp ( "nat" ) )
319
- result = right_f (Timestamp ( "nat" ) , s_nat )
309
+ expected = left_f (s_nat , NaT )
310
+ result = right_f (NaT , s_nat )
320
311
tm .assert_series_equal (result , expected )
321
312
322
313
def test_dt64arr_timestamp_equality (self , box_with_array ):
@@ -832,17 +823,6 @@ def test_dt64arr_add_timedeltalike_scalar(
832
823
result = rng + two_hours
833
824
tm .assert_equal (result , expected )
834
825
835
- def test_dt64arr_iadd_timedeltalike_scalar (
836
- self , tz_naive_fixture , two_hours , box_with_array
837
- ):
838
- tz = tz_naive_fixture
839
-
840
- rng = date_range ("2000-01-01" , "2000-02-01" , tz = tz )
841
- expected = date_range ("2000-01-01 02:00" , "2000-02-01 02:00" , tz = tz )
842
-
843
- rng = tm .box_expected (rng , box_with_array )
844
- expected = tm .box_expected (expected , box_with_array )
845
-
846
826
rng += two_hours
847
827
tm .assert_equal (rng , expected )
848
828
@@ -860,17 +840,6 @@ def test_dt64arr_sub_timedeltalike_scalar(
860
840
result = rng - two_hours
861
841
tm .assert_equal (result , expected )
862
842
863
- def test_dt64arr_isub_timedeltalike_scalar (
864
- self , tz_naive_fixture , two_hours , box_with_array
865
- ):
866
- tz = tz_naive_fixture
867
-
868
- rng = date_range ("2000-01-01" , "2000-02-01" , tz = tz )
869
- expected = date_range ("1999-12-31 22:00" , "2000-01-31 22:00" , tz = tz )
870
-
871
- rng = tm .box_expected (rng , box_with_array )
872
- expected = tm .box_expected (expected , box_with_array )
873
-
874
843
rng -= two_hours
875
844
tm .assert_equal (rng , expected )
876
845
@@ -1071,21 +1040,14 @@ def test_dt64arr_add_dt64ndarray_raises(self, tz_naive_fixture, box_with_array):
1071
1040
dt64vals = dti .values
1072
1041
1073
1042
dtarr = tm .box_expected (dti , box_with_array )
1074
- msg = "cannot add"
1075
- with pytest .raises (TypeError , match = msg ):
1076
- dtarr + dt64vals
1077
- with pytest .raises (TypeError , match = msg ):
1078
- dt64vals + dtarr
1043
+ assert_cannot_add (dtarr , dt64vals )
1079
1044
1080
1045
def test_dt64arr_add_timestamp_raises (self , box_with_array ):
1081
1046
# GH#22163 ensure DataFrame doesn't cast Timestamp to i8
1082
1047
idx = DatetimeIndex (["2011-01-01" , "2011-01-02" ])
1048
+ ts = idx [0 ]
1083
1049
idx = tm .box_expected (idx , box_with_array )
1084
- msg = "cannot add"
1085
- with pytest .raises (TypeError , match = msg ):
1086
- idx + Timestamp ("2011-01-01" )
1087
- with pytest .raises (TypeError , match = msg ):
1088
- Timestamp ("2011-01-01" ) + idx
1050
+ assert_cannot_add (idx , ts )
1089
1051
1090
1052
# -------------------------------------------------------------
1091
1053
# Other Invalid Addition/Subtraction
@@ -1267,13 +1229,12 @@ def test_dti_add_tick_tzaware(self, tz_aware_fixture, box_with_array):
1267
1229
dates = tm .box_expected (dates , box_with_array )
1268
1230
expected = tm .box_expected (expected , box_with_array )
1269
1231
1270
- # TODO: parametrize over the scalar being added? radd? sub?
1271
- offset = dates + pd .offsets .Hour (5 )
1272
- tm .assert_equal (offset , expected )
1273
- offset = dates + np .timedelta64 (5 , "h" )
1274
- tm .assert_equal (offset , expected )
1275
- offset = dates + timedelta (hours = 5 )
1276
- tm .assert_equal (offset , expected )
1232
+ # TODO: sub?
1233
+ for scalar in [pd .offsets .Hour (5 ), np .timedelta64 (5 , "h" ), timedelta (hours = 5 )]:
1234
+ offset = dates + scalar
1235
+ tm .assert_equal (offset , expected )
1236
+ offset = scalar + dates
1237
+ tm .assert_equal (offset , expected )
1277
1238
1278
1239
# -------------------------------------------------------------
1279
1240
# RelativeDelta DateOffsets
@@ -1941,30 +1902,24 @@ def test_dt64_mul_div_numeric_invalid(self, one, dt64_series):
1941
1902
one / dt64_series
1942
1903
1943
1904
# TODO: parametrize over box
1944
- @pytest .mark .parametrize ("op" , ["__add__" , "__radd__" , "__sub__" , "__rsub__" ])
1945
- def test_dt64_series_add_intlike (self , tz_naive_fixture , op ):
1905
+ def test_dt64_series_add_intlike (self , tz_naive_fixture ):
1946
1906
# GH#19123
1947
1907
tz = tz_naive_fixture
1948
1908
dti = DatetimeIndex (["2016-01-02" , "2016-02-03" , "NaT" ], tz = tz )
1949
1909
ser = Series (dti )
1950
1910
1951
1911
other = Series ([20 , 30 , 40 ], dtype = "uint8" )
1952
1912
1953
- method = getattr (ser , op )
1954
1913
msg = "|" .join (
1955
1914
[
1956
1915
"Addition/subtraction of integers and integer-arrays" ,
1957
1916
"cannot subtract .* from ndarray" ,
1958
1917
]
1959
1918
)
1960
- with pytest .raises (TypeError , match = msg ):
1961
- method (1 )
1962
- with pytest .raises (TypeError , match = msg ):
1963
- method (other )
1964
- with pytest .raises (TypeError , match = msg ):
1965
- method (np .array (other ))
1966
- with pytest .raises (TypeError , match = msg ):
1967
- method (pd .Index (other ))
1919
+ assert_invalid_addsub_type (ser , 1 , msg )
1920
+ assert_invalid_addsub_type (ser , other , msg )
1921
+ assert_invalid_addsub_type (ser , np .array (other ), msg )
1922
+ assert_invalid_addsub_type (ser , pd .Index (other ), msg )
1968
1923
1969
1924
# -------------------------------------------------------------
1970
1925
# Timezone-Centric Tests
@@ -2062,7 +2017,9 @@ def test_dti_add_intarray_tick(self, int_holder, freq):
2062
2017
dti = date_range ("2016-01-01" , periods = 2 , freq = freq )
2063
2018
other = int_holder ([4 , - 1 ])
2064
2019
2065
- msg = "Addition/subtraction of integers|cannot subtract DatetimeArray from"
2020
+ msg = "|" .join (
2021
+ ["Addition/subtraction of integers" , "cannot subtract DatetimeArray from" ]
2022
+ )
2066
2023
assert_invalid_addsub_type (dti , other , msg )
2067
2024
2068
2025
@pytest .mark .parametrize ("freq" , ["W" , "M" , "MS" , "Q" ])
@@ -2072,7 +2029,9 @@ def test_dti_add_intarray_non_tick(self, int_holder, freq):
2072
2029
dti = date_range ("2016-01-01" , periods = 2 , freq = freq )
2073
2030
other = int_holder ([4 , - 1 ])
2074
2031
2075
- msg = "Addition/subtraction of integers|cannot subtract DatetimeArray from"
2032
+ msg = "|" .join (
2033
+ ["Addition/subtraction of integers" , "cannot subtract DatetimeArray from" ]
2034
+ )
2076
2035
assert_invalid_addsub_type (dti , other , msg )
2077
2036
2078
2037
@pytest .mark .parametrize ("int_holder" , [np .array , pd .Index ])
@@ -2222,10 +2181,7 @@ def test_add_datetimelike_and_dtarr(self, box_with_array, addend, tz):
2222
2181
dtarr = tm .box_expected (dti , box_with_array )
2223
2182
msg = "cannot add DatetimeArray and"
2224
2183
2225
- with pytest .raises (TypeError , match = msg ):
2226
- dtarr + addend
2227
- with pytest .raises (TypeError , match = msg ):
2228
- addend + dtarr
2184
+ assert_cannot_add (dtarr , addend , msg )
2229
2185
2230
2186
# -------------------------------------------------------------
2231
2187
0 commit comments