|
5 | 5 | import numpy as np
|
6 | 6 | from pandas._libs import (lib, index as libindex, tslib as libts,
|
7 | 7 | algos as libalgos, join as libjoin,
|
8 |
| - Timestamp) |
| 8 | + Timestamp, Timedelta) |
9 | 9 | from pandas._libs.lib import is_datetime_array
|
10 | 10 |
|
11 | 11 | from pandas.compat import range, u, set_function_name
|
|
16 | 16 | from pandas.core.dtypes.generic import (
|
17 | 17 | ABCSeries, ABCDataFrame,
|
18 | 18 | ABCMultiIndex,
|
19 |
| - ABCPeriodIndex, |
| 19 | + ABCPeriodIndex, ABCTimedeltaIndex, |
20 | 20 | ABCDateOffset)
|
21 | 21 | from pandas.core.dtypes.missing import isna, array_equivalent
|
22 | 22 | from pandas.core.dtypes.common import (
|
@@ -3918,7 +3918,21 @@ def dropna(self, how='any'):
|
3918 | 3918 | return self._shallow_copy()
|
3919 | 3919 |
|
3920 | 3920 | def _evaluate_with_timedelta_like(self, other, op, opstr, reversed=False):
|
3921 |
| - raise TypeError("can only perform ops with timedelta like values") |
| 3921 | + # Timedelta knows how to operate with np.array, so dispatch to that |
| 3922 | + # operation and then wrap the results |
| 3923 | + other = Timedelta(other) |
| 3924 | + values = self.values |
| 3925 | + if reversed: |
| 3926 | + values, other = other, values |
| 3927 | + |
| 3928 | + with np.errstate(all='ignore'): |
| 3929 | + result = op(values, other) |
| 3930 | + |
| 3931 | + attrs = self._get_attributes_dict() |
| 3932 | + attrs = self._maybe_update_attributes(attrs) |
| 3933 | + if op == divmod: |
| 3934 | + return Index(result[0], **attrs), Index(result[1], **attrs) |
| 3935 | + return Index(result, **attrs) |
3922 | 3936 |
|
3923 | 3937 | def _evaluate_with_datetime_like(self, other, op, opstr):
|
3924 | 3938 | raise TypeError("can only perform ops with datetime like values")
|
@@ -4061,6 +4075,9 @@ def _make_evaluate_binop(op, opstr, reversed=False, constructor=Index):
|
4061 | 4075 | def _evaluate_numeric_binop(self, other):
|
4062 | 4076 | if isinstance(other, (ABCSeries, ABCDataFrame)):
|
4063 | 4077 | return NotImplemented
|
| 4078 | + elif isinstance(other, ABCTimedeltaIndex): |
| 4079 | + # Defer to subclass implementation |
| 4080 | + return NotImplemented |
4064 | 4081 |
|
4065 | 4082 | other = self._validate_for_numeric_binop(other, op, opstr)
|
4066 | 4083 |
|
|
0 commit comments