@@ -31,8 +31,6 @@ import_datetime()
31
31
32
32
33
33
cimport pandas._libs.tslibs.util as util
34
- from pandas._libs cimport ops
35
- from pandas._libs.missing cimport C_NA
36
34
from pandas._libs.tslibs.base cimport ABCTimestamp
37
35
from pandas._libs.tslibs.conversion cimport (
38
36
cast_from_unit,
@@ -217,12 +215,11 @@ cpdef int64_t delta_to_nanoseconds(delta) except? -1:
217
215
return get_timedelta64_value(ensure_td64ns(delta))
218
216
219
217
if PyDelta_Check(delta):
220
- microseconds = (
218
+ return (
221
219
delta.days * 24 * 3600 * 1 _000_000
222
220
+ delta.seconds * 1 _000_000
223
221
+ delta.microseconds
224
- )
225
- return calc_int_int(operator.mul, microseconds, 1000 )
222
+ ) * 1000
226
223
227
224
raise TypeError (type (delta))
228
225
@@ -245,14 +242,20 @@ cdef object ensure_td64ns(object ts):
245
242
str unitstr
246
243
247
244
td64_unit = get_datetime64_unit(ts)
248
- if td64_unit in ( NPY_DATETIMEUNIT.NPY_FR_ns, NPY_DATETIMEUNIT.NPY_FR_GENERIC) :
245
+ if td64_unit == NPY_DATETIMEUNIT.NPY_FR_ns or td64_unit == NPY_DATETIMEUNIT.NPY_FR_GENERIC:
249
246
return ts
250
247
251
248
unitstr = npy_unit_to_abbrev(td64_unit)
252
249
mult = precision_from_unit(unitstr)[0 ]
253
- ns = calc_int_int(operator.mul, get_timedelta64_value(ts), mult)
254
250
255
- return np.timedelta64(ns, " ns" )
251
+ with cython.overflowcheck(True ):
252
+ try :
253
+ td64_value = get_timedelta64_value(ts) * mult
254
+ except OverflowError as ex:
255
+ msg = f" {ts} outside allowed range [{TIMEDELTA_MIN_NS}ns, {TIMEDELTA_MAX_NS}ns]"
256
+ raise OutOfBoundsTimedelta(msg) from ex
257
+
258
+ return np.timedelta64(td64_value, " ns" )
256
259
257
260
258
261
cdef convert_to_timedelta64(object ts, str unit):
@@ -673,18 +676,6 @@ def _op_unary_method(func, name):
673
676
return f
674
677
675
678
676
- cdef int64_t calc_int_int(object op, object a, object b) except ? - 1 :
677
- """
678
- Calculate op(a, b), raising if either operand or the result cannot be safely cast
679
- to an int64_t.
680
- """
681
- try :
682
- return ops.calc_int_int(op, a, b)
683
- except OverflowError as ex:
684
- msg = f" outside allowed range [{TIMEDELTA_MIN_NS}ns, {TIMEDELTA_MAX_NS}ns]"
685
- raise OutOfBoundsTimedelta(msg) from ex
686
-
687
-
688
679
def _binary_op_method_timedeltalike (op , name ):
689
680
# define a binary operation that only works if the other argument is
690
681
# timedelta like or an array of timedeltalike
@@ -907,8 +898,6 @@ cdef object create_timedelta(object value, str in_unit, NPY_DATETIMEUNIT out_res
907
898
908
899
if isinstance (value, _Timedelta):
909
900
return value
910
- if value is C_NA:
911
- raise ValueError (" Not supported" )
912
901
913
902
try :
914
903
# if unit == "ns", no need to create an m8[ns] just to read the (same) value back
0 commit comments