Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f8ce4ca

Browse files
authoredMay 31, 2020
CLN: de-duplicate bits lib timestamps (#34491)
1 parent a95116d commit f8ce4ca

File tree

2 files changed

+15
-35
lines changed

2 files changed

+15
-35
lines changed
 

‎pandas/_libs/tslibs/timestamps.pyx

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ from pandas._libs.tslibs.util cimport (
2525
is_timedelta64_object, is_array,
2626
)
2727

28-
from pandas._libs.tslibs.base cimport ABCTimedelta, ABCTimestamp
28+
from pandas._libs.tslibs.base cimport ABCTimestamp
2929

3030
from pandas._libs.tslibs cimport ccalendar
3131

@@ -41,6 +41,7 @@ from pandas._libs.tslibs.np_datetime cimport (
4141
)
4242
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
4343
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
4445
from pandas._libs.tslibs.timedeltas import Timedelta
4546
from pandas._libs.tslibs.timezones cimport (
4647
is_utc, maybe_get_tz, treat_tz_as_pytz, utc_pytz as UTC,
@@ -344,37 +345,15 @@ cdef class _Timestamp(ABCTimestamp):
344345

345346
def __add__(self, other):
346347
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
365349

350+
if is_any_td_scalar(other):
351+
nanos = delta_to_nanoseconds(other)
366352
result = type(self)(self.value + nanos, tz=self.tzinfo, freq=self.freq)
367353
return result
368354

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)
378357

379358
elif is_array(other):
380359
if other.dtype.kind in ['i', 'u']:
@@ -395,8 +374,7 @@ cdef class _Timestamp(ABCTimestamp):
395374

396375
def __sub__(self, other):
397376

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):
400378
neg_other = -other
401379
return self + neg_other
402380

@@ -434,7 +412,6 @@ cdef class _Timestamp(ABCTimestamp):
434412

435413
# scalar Timestamp/datetime - Timestamp/datetime -> yields a
436414
# Timedelta
437-
from pandas._libs.tslibs.timedeltas import Timedelta
438415
try:
439416
return Timedelta(self.value - other.value)
440417
except (OverflowError, OutOfBoundsDatetime) as err:

‎pandas/tests/scalar/timestamp/test_arithmetic.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,17 @@ def test_overflow_offset_raises(self):
3838
r"\<-?\d+ \* Days\> and \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} "
3939
"will overflow"
4040
)
41+
lmsg = "|".join(
42+
["Python int too large to convert to C long", "int too big to convert"]
43+
)
4144

42-
with pytest.raises(OverflowError, match=msg):
45+
with pytest.raises(OverflowError, match=lmsg):
4346
stamp + offset_overflow
4447

4548
with pytest.raises(OverflowError, match=msg):
4649
offset_overflow + stamp
4750

48-
with pytest.raises(OverflowError, match=msg):
51+
with pytest.raises(OverflowError, match=lmsg):
4952
stamp - offset_overflow
5053

5154
# xref https://github.com/pandas-dev/pandas/issues/14080
@@ -54,13 +57,13 @@ def test_overflow_offset_raises(self):
5457
stamp = Timestamp("2000/1/1")
5558
offset_overflow = to_offset("D") * 100 ** 5
5659

57-
with pytest.raises(OverflowError, match=msg):
60+
with pytest.raises(OverflowError, match=lmsg):
5861
stamp + offset_overflow
5962

6063
with pytest.raises(OverflowError, match=msg):
6164
offset_overflow + stamp
6265

63-
with pytest.raises(OverflowError, match=msg):
66+
with pytest.raises(OverflowError, match=lmsg):
6467
stamp - offset_overflow
6568

6669
def test_overflow_timestamp_raises(self):

0 commit comments

Comments
 (0)
Please sign in to comment.