@@ -1129,56 +1129,46 @@ def _sub_period(self, other):
1129
1129
def _add_offset (self , offset ):
1130
1130
raise AbstractMethodError (self )
1131
1131
1132
- def _add_delta (self , other ):
1132
+ def _add_timedeltalike_scalar (self , other ):
1133
1133
"""
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
1141
1135
1142
1136
Returns
1143
1137
-------
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
1163
1139
"""
1164
1140
if isna (other ):
1165
1141
# i.e np.timedelta64("NaT"), not recognized by delta_to_nanoseconds
1166
1142
new_values = np .empty (self .shape , dtype = "i8" )
1167
1143
new_values [:] = iNaT
1168
- return new_values
1144
+ return type ( self )( new_values , dtype = self . dtype )
1169
1145
1170
1146
inc = delta_to_nanoseconds (other )
1171
1147
new_values = checked_add_with_arr (self .asi8 , inc , arr_mask = self ._isnan ).view (
1172
1148
"i8"
1173
1149
)
1174
1150
new_values = self ._maybe_mask_results (new_values )
1175
- return new_values .view ("i8" )
1176
1151
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 ):
1178
1163
"""
1179
1164
Add a delta of a TimedeltaIndex
1180
- return the i8 result view
1165
+
1166
+ Returns
1167
+ -------
1168
+ Same type as self
1181
1169
"""
1170
+ # overriden by PeriodArray
1171
+
1182
1172
if len (self ) != len (other ):
1183
1173
raise ValueError ("cannot add indices of unequal length" )
1184
1174
@@ -1196,7 +1186,8 @@ def _add_delta_tdi(self, other):
1196
1186
if self ._hasnans or other ._hasnans :
1197
1187
mask = (self ._isnan ) | (other ._isnan )
1198
1188
new_values [mask ] = iNaT
1199
- return new_values .view ("i8" )
1189
+
1190
+ return type (self )._from_sequence (new_values , dtype = self .dtype , freq = "infer" )
1200
1191
1201
1192
def _add_nat (self ):
1202
1193
"""
@@ -1338,7 +1329,7 @@ def __add__(self, other):
1338
1329
if other is NaT :
1339
1330
result = self ._add_nat ()
1340
1331
elif isinstance (other , (Tick , timedelta , np .timedelta64 )):
1341
- result = self ._add_delta (other )
1332
+ result = self ._add_timedeltalike_scalar (other )
1342
1333
elif isinstance (other , DateOffset ):
1343
1334
# specifically _not_ a Tick
1344
1335
result = self ._add_offset (other )
@@ -1354,7 +1345,7 @@ def __add__(self, other):
1354
1345
# array-like others
1355
1346
elif is_timedelta64_dtype (other ):
1356
1347
# TimedeltaIndex, ndarray[timedelta64]
1357
- result = self ._add_delta (other )
1348
+ result = self ._add_timedelta_arraylike (other )
1358
1349
elif is_object_dtype (other ):
1359
1350
# e.g. Array/Index of DateOffset objects
1360
1351
result = self ._addsub_object_array (other , operator .add )
@@ -1390,7 +1381,7 @@ def __sub__(self, other):
1390
1381
if other is NaT :
1391
1382
result = self ._sub_nat ()
1392
1383
elif isinstance (other , (Tick , timedelta , np .timedelta64 )):
1393
- result = self ._add_delta (- other )
1384
+ result = self ._add_timedeltalike_scalar (- other )
1394
1385
elif isinstance (other , DateOffset ):
1395
1386
# specifically _not_ a Tick
1396
1387
result = self ._add_offset (- other )
@@ -1409,7 +1400,7 @@ def __sub__(self, other):
1409
1400
# array-like others
1410
1401
elif is_timedelta64_dtype (other ):
1411
1402
# TimedeltaIndex, ndarray[timedelta64]
1412
- result = self ._add_delta (- other )
1403
+ result = self ._add_timedelta_arraylike (- other )
1413
1404
elif is_object_dtype (other ):
1414
1405
# e.g. Array/Index of DateOffset objects
1415
1406
result = self ._addsub_object_array (other , operator .sub )
0 commit comments