@@ -19,7 +19,8 @@ from pandas.compat import PY2
1919
2020cimport cython
2121
22- from cpython.datetime cimport PyDateTime_Check, PyDateTime_IMPORT
22+ from cpython.datetime cimport (PyDateTime_Check, PyDelta_Check,
23+ PyDateTime_IMPORT)
2324# import datetime C API
2425PyDateTime_IMPORT
2526
@@ -1058,18 +1059,21 @@ cdef class _Period(object):
10581059 return hash ((self .ordinal, self .freqstr))
10591060
10601061 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)):
10621067 offset = frequencies.to_offset(self .freq.rule_code)
10631068 if isinstance (offset, offsets.Tick):
10641069 nanos = delta_to_nanoseconds(other)
10651070 offset_nanos = delta_to_nanoseconds(offset)
1066-
10671071 if nanos % offset_nanos == 0 :
10681072 ordinal = self .ordinal + (nanos // offset_nanos)
10691073 return Period(ordinal = ordinal, freq = self .freq)
10701074 msg = ' Input cannot be converted to Period(freq={0})'
10711075 raise IncompatibleFrequency(msg.format(self .freqstr))
1072- elif isinstance (other, offsets.DateOffset ):
1076+ elif util.is_offset_object (other):
10731077 freqstr = other.rule_code
10741078 base = get_base_alias(freqstr)
10751079 if base == self .freq.rule_code:
@@ -1082,8 +1086,8 @@ cdef class _Period(object):
10821086
10831087 def __add__ (self , other ):
10841088 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 )):
10871091 return self ._add_delta(other)
10881092 elif other is NaT:
10891093 return NaT
@@ -1109,8 +1113,8 @@ cdef class _Period(object):
11091113
11101114 def __sub__ (self , other ):
11111115 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 )):
11141118 neg_other = - other
11151119 return self + neg_other
11161120 elif util.is_integer_object(other):
0 commit comments