@@ -91,6 +91,7 @@ def wrapper(self, other, name=name):
91
91
92
92
is_timedelta_lhs = com .is_timedelta64_dtype (self )
93
93
is_datetime_lhs = com .is_datetime64_dtype (self )
94
+ is_integer_lhs = lvalues .dtype == np .int64
94
95
95
96
if is_datetime_lhs or is_timedelta_lhs :
96
97
@@ -115,8 +116,8 @@ def convert_to_array(values):
115
116
# py3 compat where dtype is 'm' but is an integer
116
117
if values .dtype .kind == 'm' :
117
118
values = values .astype ('timedelta64[ns]' )
118
- else :
119
- raise ValueError ("incompatible type for a datetime/timedelta operation" )
119
+ elif name not in [ '__div__' , '__mul__' ] :
120
+ raise TypeError ("incompatible type for a datetime/timedelta operation" )
120
121
elif isinstance (values [0 ],DateOffset ):
121
122
# handle DateOffsets
122
123
values = pa .array ([ v .delta for v in values ])
@@ -131,9 +132,22 @@ def convert_to_array(values):
131
132
132
133
is_datetime_rhs = com .is_datetime64_dtype (rvalues )
133
134
is_timedelta_rhs = com .is_timedelta64_dtype (rvalues ) or (not is_datetime_rhs and _np_version_under1p7 )
135
+ is_integer_rhs = rvalues .dtype == np .int64
136
+ mask = None
137
+
138
+ # timedelta and integer mul/div
139
+ if (is_timedelta_lhs and is_integer_rhs ) or (is_integer_lhs and is_timedelta_rhs ):
140
+
141
+ if name not in ['__div__' ,'__mul__' ]:
142
+ raise TypeError ("can only operate on a timedelta and an integer for "
143
+ "division, but the operator [%s] was passed" % name )
144
+ dtype = 'timedelta64[ns]'
145
+ mask = isnull (lvalues ) | isnull (rvalues )
146
+ lvalues = lvalues .astype (np .int64 )
147
+ rvalues = rvalues .astype (np .int64 )
134
148
135
149
# 2 datetimes or 2 timedeltas
136
- if (is_timedelta_lhs and is_timedelta_rhs ) or (is_datetime_lhs and
150
+ elif (is_timedelta_lhs and is_timedelta_rhs ) or (is_datetime_lhs and
137
151
is_datetime_rhs ):
138
152
if is_datetime_lhs and name != '__sub__' :
139
153
raise TypeError ("can only operate on a datetimes for subtraction, "
@@ -143,13 +157,7 @@ def convert_to_array(values):
143
157
"addition and subtraction, but the operator [%s] was passed" % name )
144
158
145
159
dtype = 'timedelta64[ns]'
146
-
147
160
mask = isnull (lvalues ) | isnull (rvalues )
148
- if mask .any ():
149
- def wrap_results (x ):
150
- x = pa .array (x ,dtype = 'timedelta64[ns]' )
151
- np .putmask (x ,mask ,tslib .iNaT )
152
- return x
153
161
154
162
# datetime and timedelta
155
163
elif is_timedelta_rhs and is_datetime_lhs :
@@ -167,9 +175,18 @@ def wrap_results(x):
167
175
dtype = 'M8[ns]'
168
176
169
177
else :
170
- raise ValueError ('cannot operate on a series with out a rhs '
171
- 'of a series/ndarray of type datetime64[ns] '
172
- 'or a timedelta' )
178
+ raise TypeError ('cannot operate on a series with out a rhs '
179
+ 'of a series/ndarray of type datetime64[ns] '
180
+ 'or a timedelta' )
181
+
182
+ # if we need to mask the results
183
+ if mask is not None :
184
+ if mask .any ():
185
+ def f (x ):
186
+ x = pa .array (x ,dtype = 'timedelta64[ns]' )
187
+ np .putmask (x ,mask ,tslib .iNaT )
188
+ return x
189
+ wrap_results = f
173
190
174
191
lvalues = lvalues .view ('i8' )
175
192
rvalues = rvalues .view ('i8' )
0 commit comments