@@ -25,7 +25,7 @@ from pandas._libs.tslibs.util cimport (
25
25
is_timedelta64_object, is_array,
26
26
)
27
27
28
- from pandas._libs.tslibs.base cimport ABCTimedelta, ABCTimestamp
28
+ from pandas._libs.tslibs.base cimport ABCTimestamp
29
29
30
30
from pandas._libs.tslibs cimport ccalendar
31
31
@@ -41,6 +41,7 @@ from pandas._libs.tslibs.np_datetime cimport (
41
41
)
42
42
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
43
43
from pandas._libs.tslibs.offsets cimport to_offset, is_tick_object, is_offset_object
44
+ from pandas._libs.tslibs.timedeltas cimport is_any_td_scalar, delta_to_nanoseconds
44
45
from pandas._libs.tslibs.timedeltas import Timedelta
45
46
from pandas._libs.tslibs.timezones cimport (
46
47
is_utc, maybe_get_tz, treat_tz_as_pytz, utc_pytz as UTC,
@@ -344,37 +345,15 @@ cdef class _Timestamp(ABCTimestamp):
344
345
345
346
def __add__(self , other ):
346
347
cdef:
347
- int64_t other_int, nanos = 0
348
-
349
- if is_timedelta64_object(other):
350
- other_int = other.astype(' timedelta64[ns]' ).view(' i8' )
351
- return type (self )(self .value + other_int, tz = self .tzinfo, freq = self .freq)
352
-
353
- elif is_integer_object(other):
354
- raise integer_op_not_supported(self )
355
-
356
- elif PyDelta_Check(other):
357
- # logic copied from delta_to_nanoseconds to prevent circular import
358
- if isinstance (other, ABCTimedelta):
359
- # pd.Timedelta
360
- nanos = other.value
361
- else :
362
- nanos = (other.days * 24 * 60 * 60 * 1000000 +
363
- other.seconds * 1000000 +
364
- other.microseconds) * 1000
348
+ int64_t nanos = 0
365
349
350
+ if is_any_td_scalar(other):
351
+ nanos = delta_to_nanoseconds(other)
366
352
result = type (self )(self .value + nanos, tz = self .tzinfo, freq = self .freq)
367
353
return result
368
354
369
- elif is_tick_object(other):
370
- try :
371
- nanos = other.nanos
372
- except OverflowError as err:
373
- raise OverflowError (
374
- f" the add operation between {other} and {self} will overflow"
375
- ) from err
376
- result = type (self )(self .value + nanos, tz = self .tzinfo, freq = self .freq)
377
- return result
355
+ elif is_integer_object(other):
356
+ raise integer_op_not_supported(self )
378
357
379
358
elif is_array(other):
380
359
if other.dtype.kind in [' i' , ' u' ]:
@@ -395,8 +374,7 @@ cdef class _Timestamp(ABCTimestamp):
395
374
396
375
def __sub__ (self , other ):
397
376
398
- if (is_timedelta64_object(other) or is_integer_object(other) or
399
- PyDelta_Check(other) or is_tick_object(other)):
377
+ if is_any_td_scalar(other) or is_integer_object(other):
400
378
neg_other = - other
401
379
return self + neg_other
402
380
@@ -434,7 +412,6 @@ cdef class _Timestamp(ABCTimestamp):
434
412
435
413
# scalar Timestamp/datetime - Timestamp/datetime -> yields a
436
414
# Timedelta
437
- from pandas._libs.tslibs.timedeltas import Timedelta
438
415
try :
439
416
return Timedelta(self .value - other.value)
440
417
except (OverflowError , OutOfBoundsDatetime) as err:
0 commit comments