27
27
is_integer_dtype ,
28
28
is_list_like ,
29
29
is_object_dtype ,
30
- is_offsetlike ,
31
30
is_period_dtype ,
32
31
is_string_dtype ,
33
32
is_timedelta64_dtype ,
@@ -1075,8 +1074,6 @@ def _sub_period_array(self, other):
1075
1074
f"cannot subtract { other .dtype } -dtype from { type (self ).__name__ } "
1076
1075
)
1077
1076
1078
- if len (self ) != len (other ):
1079
- raise ValueError ("cannot subtract arrays/indices of unequal length" )
1080
1077
if self .freq != other .freq :
1081
1078
msg = DIFFERENT_FREQ .format (
1082
1079
cls = type (self ).__name__ , own_freq = self .freqstr , other_freq = other .freqstr
@@ -1093,14 +1090,13 @@ def _sub_period_array(self, other):
1093
1090
new_values [mask ] = NaT
1094
1091
return new_values
1095
1092
1096
- def _addsub_offset_array (self , other , op ):
1093
+ def _addsub_object_array (self , other : np . ndarray , op ):
1097
1094
"""
1098
1095
Add or subtract array-like of DateOffset objects
1099
1096
1100
1097
Parameters
1101
1098
----------
1102
- other : Index, np.ndarray
1103
- object-dtype containing pd.DateOffset objects
1099
+ other : np.ndarray[object]
1104
1100
op : {operator.add, operator.sub}
1105
1101
1106
1102
Returns
@@ -1124,7 +1120,12 @@ def _addsub_offset_array(self, other, op):
1124
1120
kwargs = {}
1125
1121
if not is_period_dtype (self ):
1126
1122
kwargs ["freq" ] = "infer"
1127
- return self ._from_sequence (res_values , ** kwargs )
1123
+ try :
1124
+ res = type (self )._from_sequence (res_values , ** kwargs )
1125
+ except ValueError :
1126
+ # e.g. we've passed a Timestamp to TimedeltaArray
1127
+ res = res_values
1128
+ return res
1128
1129
1129
1130
def _time_shift (self , periods , freq = None ):
1130
1131
"""
@@ -1187,9 +1188,9 @@ def __add__(self, other):
1187
1188
elif is_timedelta64_dtype (other ):
1188
1189
# TimedeltaIndex, ndarray[timedelta64]
1189
1190
result = self ._add_delta (other )
1190
- elif is_offsetlike (other ):
1191
- # Array/Index of DateOffset objects
1192
- result = self ._addsub_offset_array (other , operator .add )
1191
+ elif is_object_dtype (other ):
1192
+ # e.g. Array/Index of DateOffset objects
1193
+ result = self ._addsub_object_array (other , operator .add )
1193
1194
elif is_datetime64_dtype (other ) or is_datetime64tz_dtype (other ):
1194
1195
# DatetimeIndex, ndarray[datetime64]
1195
1196
return self ._add_datetime_arraylike (other )
@@ -1242,9 +1243,9 @@ def __sub__(self, other):
1242
1243
elif is_timedelta64_dtype (other ):
1243
1244
# TimedeltaIndex, ndarray[timedelta64]
1244
1245
result = self ._add_delta (- other )
1245
- elif is_offsetlike (other ):
1246
- # Array/Index of DateOffset objects
1247
- result = self ._addsub_offset_array (other , operator .sub )
1246
+ elif is_object_dtype (other ):
1247
+ # e.g. Array/Index of DateOffset objects
1248
+ result = self ._addsub_object_array (other , operator .sub )
1248
1249
elif is_datetime64_dtype (other ) or is_datetime64tz_dtype (other ):
1249
1250
# DatetimeIndex, ndarray[datetime64]
1250
1251
result = self ._sub_datetime_arraylike (other )
0 commit comments