@@ -1129,56 +1129,46 @@ def _sub_period(self, other):
11291129 def _add_offset (self , offset ):
11301130 raise AbstractMethodError (self )
11311131
1132- def _add_delta (self , other ):
1132+ def _add_timedeltalike_scalar (self , other ):
11331133 """
1134- Add a timedelta-like, Tick or TimedeltaIndex-like object
1135- to self, yielding an int64 numpy array
1136-
1137- Parameters
1138- ----------
1139- delta : {timedelta, np.timedelta64, Tick,
1140- TimedeltaIndex, ndarray[timedelta64]}
1134+ Add a delta of a timedeltalike
11411135
11421136 Returns
11431137 -------
1144- result : ndarray[int64]
1145-
1146- Notes
1147- -----
1148- The result's name is set outside of _add_delta by the calling
1149- method (__add__ or __sub__), if necessary (i.e. for Indexes).
1150- """
1151- if isinstance (other , (Tick , timedelta , np .timedelta64 )):
1152- new_values = self ._add_timedeltalike_scalar (other )
1153- elif is_timedelta64_dtype (other ):
1154- # ndarray[timedelta64] or TimedeltaArray/index
1155- new_values = self ._add_delta_tdi (other )
1156-
1157- return new_values
1158-
1159- def _add_timedeltalike_scalar (self , other ):
1160- """
1161- Add a delta of a timedeltalike
1162- return the i8 result view
1138+ Same type as self
11631139 """
11641140 if isna (other ):
11651141 # i.e np.timedelta64("NaT"), not recognized by delta_to_nanoseconds
11661142 new_values = np .empty (self .shape , dtype = "i8" )
11671143 new_values [:] = iNaT
1168- return new_values
1144+ return type ( self )( new_values , dtype = self . dtype )
11691145
11701146 inc = delta_to_nanoseconds (other )
11711147 new_values = checked_add_with_arr (self .asi8 , inc , arr_mask = self ._isnan ).view (
11721148 "i8"
11731149 )
11741150 new_values = self ._maybe_mask_results (new_values )
1175- return new_values .view ("i8" )
11761151
1177- def _add_delta_tdi (self , other ):
1152+ new_freq = None
1153+ if isinstance (self .freq , Tick ) or is_period_dtype (self .dtype ):
1154+ # adding a scalar preserves freq
1155+ new_freq = self .freq
1156+
1157+ if new_freq is not None :
1158+ # fastpath that doesnt require inference
1159+ return type (self )(new_values , dtype = self .dtype , freq = new_freq )
1160+ return type (self )._from_sequence (new_values , dtype = self .dtype , freq = "infer" )
1161+
1162+ def _add_timedelta_arraylike (self , other ):
11781163 """
11791164 Add a delta of a TimedeltaIndex
1180- return the i8 result view
1165+
1166+ Returns
1167+ -------
1168+ Same type as self
11811169 """
1170+ # overriden by PeriodArray
1171+
11821172 if len (self ) != len (other ):
11831173 raise ValueError ("cannot add indices of unequal length" )
11841174
@@ -1196,7 +1186,8 @@ def _add_delta_tdi(self, other):
11961186 if self ._hasnans or other ._hasnans :
11971187 mask = (self ._isnan ) | (other ._isnan )
11981188 new_values [mask ] = iNaT
1199- return new_values .view ("i8" )
1189+
1190+ return type (self )._from_sequence (new_values , dtype = self .dtype , freq = "infer" )
12001191
12011192 def _add_nat (self ):
12021193 """
@@ -1338,7 +1329,7 @@ def __add__(self, other):
13381329 if other is NaT :
13391330 result = self ._add_nat ()
13401331 elif isinstance (other , (Tick , timedelta , np .timedelta64 )):
1341- result = self ._add_delta (other )
1332+ result = self ._add_timedeltalike_scalar (other )
13421333 elif isinstance (other , DateOffset ):
13431334 # specifically _not_ a Tick
13441335 result = self ._add_offset (other )
@@ -1354,7 +1345,7 @@ def __add__(self, other):
13541345 # array-like others
13551346 elif is_timedelta64_dtype (other ):
13561347 # TimedeltaIndex, ndarray[timedelta64]
1357- result = self ._add_delta (other )
1348+ result = self ._add_timedelta_arraylike (other )
13581349 elif is_object_dtype (other ):
13591350 # e.g. Array/Index of DateOffset objects
13601351 result = self ._addsub_object_array (other , operator .add )
@@ -1390,7 +1381,7 @@ def __sub__(self, other):
13901381 if other is NaT :
13911382 result = self ._sub_nat ()
13921383 elif isinstance (other , (Tick , timedelta , np .timedelta64 )):
1393- result = self ._add_delta (- other )
1384+ result = self ._add_timedeltalike_scalar (- other )
13941385 elif isinstance (other , DateOffset ):
13951386 # specifically _not_ a Tick
13961387 result = self ._add_offset (- other )
@@ -1409,7 +1400,7 @@ def __sub__(self, other):
14091400 # array-like others
14101401 elif is_timedelta64_dtype (other ):
14111402 # TimedeltaIndex, ndarray[timedelta64]
1412- result = self ._add_delta (- other )
1403+ result = self ._add_timedelta_arraylike (- other )
14131404 elif is_object_dtype (other ):
14141405 # e.g. Array/Index of DateOffset objects
14151406 result = self ._addsub_object_array (other , operator .sub )
0 commit comments