|
19 | 19 | #----------------------------------------------------------------------
|
20 | 20 | # DateOffset
|
21 | 21 |
|
| 22 | +class ApplyTypeError(TypeError): |
| 23 | + # sentinel class for catching the apply error to return NotImplemented |
| 24 | + pass |
| 25 | + |
22 | 26 |
|
23 | 27 | class CacheableOffset(object):
|
24 | 28 |
|
@@ -128,15 +132,15 @@ def __repr__(self):
|
128 | 132 | kwds_new[key] = self.kwds[key]
|
129 | 133 | if len(kwds_new) > 0:
|
130 | 134 | attrs.append('='.join((attr, repr(kwds_new))))
|
131 |
| - else: |
| 135 | + else: |
132 | 136 | if attr not in exclude:
|
133 | 137 | attrs.append('='.join((attr, repr(getattr(self, attr)))))
|
134 | 138 |
|
135 | 139 | if abs(self.n) != 1:
|
136 | 140 | plural = 's'
|
137 | 141 | else:
|
138 | 142 | plural = ''
|
139 |
| - |
| 143 | + |
140 | 144 | n_str = ""
|
141 | 145 | if self.n != 1:
|
142 | 146 | n_str = "%s * " % self.n
|
@@ -170,19 +174,21 @@ def __call__(self, other):
|
170 | 174 | return self.apply(other)
|
171 | 175 |
|
172 | 176 | def __add__(self, other):
|
173 |
| - return self.apply(other) |
| 177 | + try: |
| 178 | + return self.apply(other) |
| 179 | + except ApplyTypeError: |
| 180 | + return NotImplemented |
174 | 181 |
|
175 | 182 | def __radd__(self, other):
|
176 | 183 | return self.__add__(other)
|
177 | 184 |
|
178 | 185 | def __sub__(self, other):
|
179 | 186 | if isinstance(other, datetime):
|
180 |
| - raise TypeError('Cannot subtract datetime from offset!') |
| 187 | + raise TypeError('Cannot subtract datetime from offset.') |
181 | 188 | elif type(other) == type(self):
|
182 | 189 | return self.__class__(self.n - other.n, **self.kwds)
|
183 | 190 | else: # pragma: no cover
|
184 |
| - raise TypeError('Cannot subtract %s from %s' |
185 |
| - % (type(other), type(self))) |
| 191 | + return NotImplemented |
186 | 192 |
|
187 | 193 | def __rsub__(self, other):
|
188 | 194 | return self.__class__(-self.n, **self.kwds) + other
|
@@ -273,7 +279,7 @@ def __repr__(self): #TODO: Figure out if this should be merged into DateOffset
|
273 | 279 | plural = 's'
|
274 | 280 | else:
|
275 | 281 | plural = ''
|
276 |
| - |
| 282 | + |
277 | 283 | n_str = ""
|
278 | 284 | if self.n != 1:
|
279 | 285 | n_str = "%s * " % self.n
|
@@ -370,8 +376,8 @@ def apply(self, other):
|
370 | 376 | return BDay(self.n, offset=self.offset + other,
|
371 | 377 | normalize=self.normalize)
|
372 | 378 | else:
|
373 |
| - raise TypeError('Only know how to combine business day with ' |
374 |
| - 'datetime or timedelta!') |
| 379 | + raise ApplyTypeError('Only know how to combine business day with ' |
| 380 | + 'datetime or timedelta.') |
375 | 381 |
|
376 | 382 | @classmethod
|
377 | 383 | def onOffset(cls, dt):
|
@@ -463,8 +469,8 @@ def apply(self, other):
|
463 | 469 | return BDay(self.n, offset=self.offset + other,
|
464 | 470 | normalize=self.normalize)
|
465 | 471 | else:
|
466 |
| - raise TypeError('Only know how to combine trading day with ' |
467 |
| - 'datetime, datetime64 or timedelta!') |
| 472 | + raise ApplyTypeError('Only know how to combine trading day with ' |
| 473 | + 'datetime, datetime64 or timedelta.') |
468 | 474 | dt64 = self._to_dt64(other)
|
469 | 475 |
|
470 | 476 | day64 = dt64.astype('datetime64[D]')
|
@@ -1177,7 +1183,10 @@ def __add__(self, other):
|
1177 | 1183 | return type(self)(self.n + other.n)
|
1178 | 1184 | else:
|
1179 | 1185 | return _delta_to_tick(self.delta + other.delta)
|
1180 |
| - return self.apply(other) |
| 1186 | + try: |
| 1187 | + return self.apply(other) |
| 1188 | + except ApplyTypeError: |
| 1189 | + return NotImplemented |
1181 | 1190 |
|
1182 | 1191 | def __eq__(self, other):
|
1183 | 1192 | if isinstance(other, compat.string_types):
|
@@ -1220,8 +1229,8 @@ def apply(self, other):
|
1220 | 1229 | return other + self.delta
|
1221 | 1230 | elif isinstance(other, type(self)):
|
1222 | 1231 | return type(self)(self.n + other.n)
|
1223 |
| - else: # pragma: no cover |
1224 |
| - raise TypeError('Unhandled type: %s' % type(other)) |
| 1232 | + else: |
| 1233 | + raise ApplyTypeError('Unhandled type: %s' % type(other).__name__) |
1225 | 1234 |
|
1226 | 1235 | _rule_base = 'undefined'
|
1227 | 1236 |
|
|
0 commit comments