|
1 | 1 | # -*- coding: utf-8 -*-
|
| 2 | +from datetime import timedelta |
| 3 | + |
| 4 | +import numpy as np |
2 | 5 |
|
3 | 6 | from pandas._libs import tslib
|
4 | 7 | from pandas._libs.tslib import Timedelta, NaT
|
5 | 8 | from pandas._libs.tslibs.fields import get_timedelta_field
|
6 | 9 |
|
| 10 | +from pandas import compat |
| 11 | + |
7 | 12 | from pandas.core.dtypes.common import _TD_DTYPE
|
| 13 | +from pandas.core.dtypes.generic import ABCSeries |
| 14 | +from pandas.core.dtypes.missing import isna |
8 | 15 |
|
9 | 16 | from pandas.tseries.offsets import Tick
|
10 | 17 |
|
11 | 18 | from .datetimelike import DatetimeLikeArrayMixin
|
12 | 19 |
|
13 | 20 |
|
| 21 | +def _is_convertible_to_td(key): |
| 22 | + return isinstance(key, (Tick, timedelta, |
| 23 | + np.timedelta64, compat.string_types)) |
| 24 | + |
| 25 | + |
14 | 26 | def _field_accessor(name, alias, docstring=None):
|
15 | 27 | def f(self):
|
16 | 28 | values = self.asi8
|
@@ -48,9 +60,53 @@ def _sub_datelike(self, other):
|
48 | 60 | raise TypeError("cannot subtract a datelike from a {cls}"
|
49 | 61 | .format(cls=type(self).__name__))
|
50 | 62 |
|
| 63 | + def _evaluate_with_timedelta_like(self, other, op): |
| 64 | + if isinstance(other, ABCSeries): |
| 65 | + # GH#19042 |
| 66 | + return NotImplemented |
| 67 | + |
| 68 | + opstr = '__{opname}__'.format(opname=op.__name__).replace('__r', '__') |
| 69 | + # allow division by a timedelta |
| 70 | + if opstr in ['__div__', '__truediv__', '__floordiv__']: |
| 71 | + if _is_convertible_to_td(other): |
| 72 | + other = Timedelta(other) |
| 73 | + if isna(other): |
| 74 | + raise NotImplementedError( |
| 75 | + "division by pd.NaT not implemented") |
| 76 | + |
| 77 | + i8 = self.asi8 |
| 78 | + left, right = i8, other.value |
| 79 | + |
| 80 | + if opstr in ['__floordiv__']: |
| 81 | + result = op(left, right) |
| 82 | + else: |
| 83 | + result = op(left, np.float64(right)) |
| 84 | + result = self._maybe_mask_results(result, convert='float64') |
| 85 | + return result |
| 86 | + |
| 87 | + return NotImplemented |
| 88 | + |
51 | 89 | # ----------------------------------------------------------------
|
52 | 90 | # Conversion Methods - Vectorized analogues of Timedelta methods
|
53 | 91 |
|
| 92 | + def total_seconds(self): |
| 93 | + """ |
| 94 | + Return total duration of each element expressed in seconds. |
| 95 | +
|
| 96 | + This method is available directly on TimedeltaArray, TimedeltaIndex |
| 97 | + and on Series containing timedelta values under the ``.dt`` namespace. |
| 98 | +
|
| 99 | + Returns |
| 100 | + ------- |
| 101 | + seconds : ndarray, Float64Index, or Series |
| 102 | + When the calling object is a TimedeltaArray, the return type |
| 103 | + is ndarray. When the calling object is a TimedeltaIndex, |
| 104 | + the return type is a Float64Index. When the calling object |
| 105 | + is a Series, the return type is Series of type `float64` whose |
| 106 | + index is the same as the original. |
| 107 | + """ |
| 108 | + return self._maybe_mask_results(1e-9 * self.asi8) |
| 109 | + |
54 | 110 | def to_pytimedelta(self):
|
55 | 111 | """
|
56 | 112 | Return Timedelta Array/Index as object ndarray of datetime.timedelta
|
|
0 commit comments