@@ -827,16 +827,52 @@ def test_sub_datetime64_not_ns(self, box, assert_func):
827
827
res = dt64 - obj
828
828
assert_func (res , - expected )
829
829
830
- def test_operators_datetimelike (self ):
831
- def run_ops (ops , get_ser , test_ser ):
830
+ def test_operators_datetimelike_invalid (self , all_arithmetic_operators ):
831
+ # these are all TypeEror ops
832
+ op_str = all_arithmetic_operators
833
+
834
+ def check (get_ser , test_ser ):
832
835
833
836
# check that we are getting a TypeError
834
837
# with 'operate' (from core/ops.py) for the ops that are not
835
838
# defined
836
- for op_str in ops :
837
- op = getattr (get_ser , op_str , None )
838
- with tm .assert_raises_regex (TypeError , 'operate|cannot' ):
839
- op (test_ser )
839
+ op = getattr (get_ser , op_str , None )
840
+ with tm .assert_raises_regex (TypeError , 'operate|cannot' ):
841
+ op (test_ser )
842
+
843
+ # ## timedelta64 ###
844
+ td1 = Series ([timedelta (minutes = 5 , seconds = 3 )] * 3 )
845
+ td1 .iloc [2 ] = np .nan
846
+
847
+ # ## datetime64 ###
848
+ dt1 = Series ([Timestamp ('20111230' ), Timestamp ('20120101' ),
849
+ Timestamp ('20120103' )])
850
+ dt1 .iloc [2 ] = np .nan
851
+ dt2 = Series ([Timestamp ('20111231' ), Timestamp ('20120102' ),
852
+ Timestamp ('20120104' )])
853
+ if op_str not in ['__sub__' , '__rsub__' ]:
854
+ check (dt1 , dt2 )
855
+
856
+ # ## datetime64 with timetimedelta ###
857
+ # TODO(jreback) __rsub__ should raise?
858
+ if op_str not in ['__add__' , '__radd__' , '__sub__' ]:
859
+ check (dt1 , td1 )
860
+
861
+ # 8260, 10763
862
+ # datetime64 with tz
863
+ tz = 'US/Eastern'
864
+ dt1 = Series (date_range ('2000-01-01 09:00:00' , periods = 5 ,
865
+ tz = tz ), name = 'foo' )
866
+ dt2 = dt1 .copy ()
867
+ dt2 .iloc [2 ] = np .nan
868
+ td1 = Series (timedelta_range ('1 days 1 min' , periods = 5 , freq = 'H' ))
869
+ td2 = td1 .copy ()
870
+ td2 .iloc [1 ] = np .nan
871
+
872
+ if op_str not in ['__add__' , '__radd__' , '__sub__' , '__rsub__' ]:
873
+ check (dt1 , td1 )
874
+
875
+ def test_operators_datetimelike (self ):
840
876
841
877
# ## timedelta64 ###
842
878
td1 = Series ([timedelta (minutes = 5 , seconds = 3 )] * 3 )
@@ -848,47 +884,31 @@ def run_ops(ops, get_ser, test_ser):
848
884
dt1 .iloc [2 ] = np .nan
849
885
dt2 = Series ([Timestamp ('20111231' ), Timestamp ('20120102' ),
850
886
Timestamp ('20120104' )])
851
- ops = ['__add__' , '__mul__' , '__floordiv__' , '__truediv__' , '__div__' ,
852
- '__pow__' , '__radd__' , '__rmul__' , '__rfloordiv__' ,
853
- '__rtruediv__' , '__rdiv__' , '__rpow__' ]
854
- run_ops (ops , dt1 , dt2 )
855
887
dt1 - dt2
856
888
dt2 - dt1
857
889
858
890
# ## datetime64 with timetimedelta ###
859
- ops = ['__mul__' , '__floordiv__' , '__truediv__' , '__div__' , '__pow__' ,
860
- '__rmul__' , '__rfloordiv__' , '__rtruediv__' , '__rdiv__' ,
861
- '__rpow__' ]
862
- run_ops (ops , dt1 , td1 )
863
891
dt1 + td1
864
892
td1 + dt1
865
893
dt1 - td1
866
894
# TODO: Decide if this ought to work.
867
895
# td1 - dt1
868
896
869
897
# ## timetimedelta with datetime64 ###
870
- ops = ['__sub__' , '__mul__' , '__floordiv__' , '__truediv__' , '__div__' ,
871
- '__pow__' , '__rmul__' , '__rfloordiv__' , '__rtruediv__' ,
872
- '__rdiv__' , '__rpow__' ]
873
- run_ops (ops , td1 , dt1 )
874
898
td1 + dt1
875
899
dt1 + td1
876
900
877
- # 8260, 10763
878
- # datetime64 with tz
879
- ops = ['__mul__' , '__floordiv__' , '__truediv__' , '__div__' , '__pow__' ,
880
- '__rmul__' , '__rfloordiv__' , '__rtruediv__' , '__rdiv__' ,
881
- '__rpow__' ]
901
+ def test_operators_datetimelike_with_timezones (self ):
882
902
883
903
tz = 'US/Eastern'
884
904
dt1 = Series (date_range ('2000-01-01 09:00:00' , periods = 5 ,
885
905
tz = tz ), name = 'foo' )
886
906
dt2 = dt1 .copy ()
887
907
dt2 .iloc [2 ] = np .nan
908
+
888
909
td1 = Series (timedelta_range ('1 days 1 min' , periods = 5 , freq = 'H' ))
889
910
td2 = td1 .copy ()
890
911
td2 .iloc [1 ] = np .nan
891
- run_ops (ops , dt1 , td1 )
892
912
893
913
result = dt1 + td1 [0 ]
894
914
exp = (dt1 .dt .tz_localize (None ) + td1 [0 ]).dt .tz_localize (tz )
@@ -1133,25 +1153,23 @@ def test_dt64_series_arith_overflow(self):
1133
1153
res = dt - ser
1134
1154
tm .assert_series_equal (res , - expected )
1135
1155
1156
+ @pytest .mark .parametrize ('op' , ['__add__' , '__radd__' ,
1157
+ '__sub__' , '__rsub__' ])
1136
1158
@pytest .mark .parametrize ('tz' , [None , 'Asia/Tokyo' ])
1137
- def test_dt64_series_add_intlike (self , tz ):
1159
+ def test_dt64_series_add_intlike (self , tz , op ):
1138
1160
# GH#19123
1139
1161
dti = pd .DatetimeIndex (['2016-01-02' , '2016-02-03' , 'NaT' ], tz = tz )
1140
1162
ser = Series (dti )
1141
1163
1142
1164
other = Series ([20 , 30 , 40 ], dtype = 'uint8' )
1143
1165
1144
- pytest .raises (TypeError , ser .__add__ , 1 )
1145
- pytest .raises (TypeError , ser .__sub__ , 1 )
1166
+ pytest .raises (TypeError , getattr (ser , op ), 1 )
1146
1167
1147
- pytest .raises (TypeError , ser .__add__ , other )
1148
- pytest .raises (TypeError , ser .__sub__ , other )
1168
+ pytest .raises (TypeError , getattr (ser , op ), other )
1149
1169
1150
- pytest .raises (TypeError , ser .__add__ , other .values )
1151
- pytest .raises (TypeError , ser .__sub__ , other .values )
1170
+ pytest .raises (TypeError , getattr (ser , op ), other .values )
1152
1171
1153
- pytest .raises (TypeError , ser .__add__ , pd .Index (other ))
1154
- pytest .raises (TypeError , ser .__sub__ , pd .Index (other ))
1172
+ pytest .raises (TypeError , getattr (ser , op ), pd .Index (other ))
1155
1173
1156
1174
1157
1175
class TestSeriesOperators (TestData ):
0 commit comments