@@ -31,8 +31,7 @@ from pandas._libs.tslibs.np_datetime cimport (
31
31
from pandas._libs.tslibs.nattype import nat_strings
32
32
from pandas._libs.tslibs.nattype cimport (
33
33
checknull_with_nat, NPY_NAT, c_NaT as NaT)
34
- from pandas._libs.tslibs.offsets cimport to_offset
35
- from pandas._libs.tslibs.offsets import _Tick as Tick
34
+ from pandas._libs.tslibs.offsets cimport to_offset, is_tick_object
36
35
37
36
# ----------------------------------------------------------------------
38
37
# Constants
@@ -140,10 +139,10 @@ def ints_to_pytimedelta(const int64_t[:] arr, box=False):
140
139
# ----------------------------------------------------------------------
141
140
142
141
cpdef int64_t delta_to_nanoseconds(delta) except ? - 1 :
143
- if hasattr (delta, ' nanos ' ):
142
+ if is_tick_object (delta):
144
143
return delta.nanos
145
- if hasattr (delta, ' delta ' ):
146
- delta = delta.delta
144
+ if isinstance (delta, _Timedelta ):
145
+ delta = delta.value
147
146
if is_timedelta64_object(delta):
148
147
return delta.astype(" timedelta64[ns]" ).item()
149
148
if is_integer_object(delta):
@@ -204,8 +203,8 @@ cdef convert_to_timedelta64(object ts, object unit):
204
203
else :
205
204
ts = parse_timedelta_string(ts)
206
205
ts = np.timedelta64(ts)
207
- elif hasattr (ts, ' delta ' ):
208
- ts = np.timedelta64(delta_to_nanoseconds(ts) , ' ns' )
206
+ elif is_tick_object (ts):
207
+ ts = np.timedelta64(ts.nanos , ' ns' )
209
208
210
209
if PyDelta_Check(ts):
211
210
ts = np.timedelta64(delta_to_nanoseconds(ts), ' ns' )
@@ -562,12 +561,10 @@ cdef bint _validate_ops_compat(other):
562
561
# return True if we are compat with operating
563
562
if checknull_with_nat(other):
564
563
return True
565
- elif PyDelta_Check(other) or is_timedelta64_object (other):
564
+ elif is_any_td_scalar (other):
566
565
return True
567
566
elif isinstance (other, str ):
568
567
return True
569
- elif hasattr (other, ' delta' ):
570
- return True
571
568
return False
572
569
573
570
@@ -779,8 +776,7 @@ cdef class _Timedelta(timedelta):
779
776
780
777
if isinstance (other, _Timedelta):
781
778
ots = other
782
- elif (is_timedelta64_object(other) or PyDelta_Check(other)
783
- or isinstance (other, Tick)):
779
+ elif is_any_td_scalar(other):
784
780
ots = Timedelta(other)
785
781
# TODO: watch out for overflows
786
782
@@ -1249,8 +1245,8 @@ class Timedelta(_Timedelta):
1249
1245
if unit is not None :
1250
1246
value = value.astype(f' timedelta64[{unit}]' )
1251
1247
value = value.astype(' timedelta64[ns]' )
1252
- elif hasattr (value, ' delta ' ):
1253
- value = np.timedelta64(delta_to_nanoseconds( value.delta) , ' ns' )
1248
+ elif is_tick_object (value):
1249
+ value = np.timedelta64(value.nanos , ' ns' )
1254
1250
elif is_integer_object(value) or is_float_object(value):
1255
1251
# unit=None is de-facto 'ns'
1256
1252
unit = parse_timedelta_unit(unit)
@@ -1460,7 +1456,7 @@ class Timedelta(_Timedelta):
1460
1456
1461
1457
cdef bint is_any_td_scalar(object obj):
1462
1458
return (
1463
- PyDelta_Check(obj) or is_timedelta64_object(obj) or isinstance (obj, Tick )
1459
+ PyDelta_Check(obj) or is_timedelta64_object(obj) or is_tick_object (obj)
1464
1460
)
1465
1461
1466
1462
0 commit comments