@@ -19,7 +19,8 @@ from pandas.compat import PY2
19
19
20
20
cimport cython
21
21
22
- from cpython.datetime cimport PyDateTime_Check, PyDateTime_IMPORT
22
+ from cpython.datetime cimport (PyDateTime_Check, PyDelta_Check,
23
+ PyDateTime_IMPORT)
23
24
# import datetime C API
24
25
PyDateTime_IMPORT
25
26
@@ -1058,18 +1059,21 @@ cdef class _Period(object):
1058
1059
return hash ((self .ordinal, self .freqstr))
1059
1060
1060
1061
def _add_delta (self , other ):
1061
- if isinstance (other, (timedelta, np.timedelta64, offsets.Tick)):
1062
+ cdef:
1063
+ int64_t nanos, offset_nanos
1064
+
1065
+ if (PyDelta_Check(other) or util.is_timedelta64_object(other) or
1066
+ isinstance (other, offsets.Tick)):
1062
1067
offset = frequencies.to_offset(self .freq.rule_code)
1063
1068
if isinstance (offset, offsets.Tick):
1064
1069
nanos = delta_to_nanoseconds(other)
1065
1070
offset_nanos = delta_to_nanoseconds(offset)
1066
-
1067
1071
if nanos % offset_nanos == 0 :
1068
1072
ordinal = self .ordinal + (nanos // offset_nanos)
1069
1073
return Period(ordinal = ordinal, freq = self .freq)
1070
1074
msg = ' Input cannot be converted to Period(freq={0})'
1071
1075
raise IncompatibleFrequency(msg.format(self .freqstr))
1072
- elif isinstance (other, offsets.DateOffset ):
1076
+ elif util.is_offset_object (other):
1073
1077
freqstr = other.rule_code
1074
1078
base = get_base_alias(freqstr)
1075
1079
if base == self .freq.rule_code:
@@ -1082,8 +1086,8 @@ cdef class _Period(object):
1082
1086
1083
1087
def __add__ (self , other ):
1084
1088
if is_period_object(self ):
1085
- if isinstance ( other, (timedelta, np.timedelta64,
1086
- offsets.DateOffset )):
1089
+ if (PyDelta_Check( other) or util.is_timedelta64_object(other) or
1090
+ util.is_offset_object(other )):
1087
1091
return self ._add_delta(other)
1088
1092
elif other is NaT:
1089
1093
return NaT
@@ -1109,8 +1113,8 @@ cdef class _Period(object):
1109
1113
1110
1114
def __sub__ (self , other ):
1111
1115
if is_period_object(self ):
1112
- if isinstance ( other, (timedelta, np.timedelta64,
1113
- offsets.DateOffset )):
1116
+ if (PyDelta_Check( other) or util.is_timedelta64_object(other) or
1117
+ util.is_offset_object(other )):
1114
1118
neg_other = - other
1115
1119
return self + neg_other
1116
1120
elif util.is_integer_object(other):
0 commit comments