Пример #1
0
 def test_astype_object(self):
     tdi = pd.TimedeltaIndex(['1 Day', '3 Hours'])
     arr = TimedeltaArrayMixin(tdi)
     asobj = arr.astype('O')
     assert isinstance(asobj, np.ndarray)
     assert asobj.dtype == 'O'
     assert list(asobj) == list(tdi)
Пример #2
0
 def test_astype_object(self):
     tdi = pd.TimedeltaIndex(['1 Day', '3 Hours'])
     arr = TimedeltaArrayMixin(tdi)
     asobj = arr.astype('O')
     assert isinstance(asobj, np.ndarray)
     assert asobj.dtype == 'O'
     assert list(asobj) == list(tdi)
Пример #3
0
    def _add_delta(self, delta):
        """
        Add a timedelta-like, DateOffset, or TimedeltaIndex-like object
        to self.

        Parameters
        ----------
        delta : {timedelta, np.timedelta64, DateOffset,
                 TimedelaIndex, ndarray[timedelta64]}

        Returns
        -------
        result : same type as self

        Notes
        -----
        The result's name is set outside of _add_delta by the calling
        method (__add__ or __sub__)
        """
        from pandas.core.arrays.timedelta import TimedeltaArrayMixin

        if isinstance(delta, (Tick, timedelta, np.timedelta64)):
            new_values = self._add_delta_td(delta)
        elif is_timedelta64_dtype(delta):
            if not isinstance(delta, TimedeltaArrayMixin):
                delta = TimedeltaArrayMixin(delta)
            new_values = self._add_delta_tdi(delta)
        else:
            new_values = self.astype('O') + delta

        tz = 'UTC' if self.tz is not None else None
        result = type(self)(new_values, tz=tz, freq='infer')
        if self.tz is not None and self.tz is not utc:
            result = result.tz_convert(self.tz)
        return result
Пример #4
0
    def total_seconds(self):
        """
        Return total duration of each element expressed in seconds.

        This method is available directly on TimedeltaIndex and on Series
        containing timedelta values under the ``.dt`` namespace.

        Returns
        -------
        seconds : Float64Index or Series
            When the calling object is a TimedeltaIndex, the return type is a
            Float64Index. When the calling object is a Series, the return type
            is Series of type `float64` whose index is the same as the
            original.

        See Also
        --------
        datetime.timedelta.total_seconds : Standard library version
            of this method.
        TimedeltaIndex.components : Return a DataFrame with components of
            each Timedelta.

        Examples
        --------
        **Series**

        >>> s = pd.Series(pd.to_timedelta(np.arange(5), unit='d'))
        >>> s
        0   0 days
        1   1 days
        2   2 days
        3   3 days
        4   4 days
        dtype: timedelta64[ns]

        >>> s.dt.total_seconds()
        0         0.0
        1     86400.0
        2    172800.0
        3    259200.0
        4    345600.0
        dtype: float64

        **TimedeltaIndex**

        >>> idx = pd.to_timedelta(np.arange(5), unit='d')
        >>> idx
        TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
                       dtype='timedelta64[ns]', freq=None)

        >>> idx.total_seconds()
        Float64Index([0.0, 86400.0, 172800.0, 259200.00000000003, 345600.0],
                     dtype='float64')
        """
        result = TimedeltaArrayMixin.total_seconds(self)
        return Index(result, name=self.name)
Пример #5
0
    def total_seconds(self):
        """
        Return total duration of each element expressed in seconds.

        This method is available directly on TimedeltaIndex and on Series
        containing timedelta values under the ``.dt`` namespace.

        Returns
        -------
        seconds : Float64Index or Series
            When the calling object is a TimedeltaIndex, the return type is a
            Float64Index. When the calling object is a Series, the return type
            is Series of type `float64` whose index is the same as the
            original.

        See Also
        --------
        datetime.timedelta.total_seconds : Standard library version
            of this method.
        TimedeltaIndex.components : Return a DataFrame with components of
            each Timedelta.

        Examples
        --------
        **Series**

        >>> s = pd.Series(pd.to_timedelta(np.arange(5), unit='d'))
        >>> s
        0   0 days
        1   1 days
        2   2 days
        3   3 days
        4   4 days
        dtype: timedelta64[ns]

        >>> s.dt.total_seconds()
        0         0.0
        1     86400.0
        2    172800.0
        3    259200.0
        4    345600.0
        dtype: float64

        **TimedeltaIndex**

        >>> idx = pd.to_timedelta(np.arange(5), unit='d')
        >>> idx
        TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
                       dtype='timedelta64[ns]', freq=None)

        >>> idx.total_seconds()
        Float64Index([0.0, 86400.0, 172800.0, 259200.00000000003, 345600.0],
                     dtype='float64')
        """
        result = TimedeltaArrayMixin.total_seconds(self)
        return Index(result, name=self.name)
Пример #6
0
 def _evaluate_with_timedelta_like(self, other, op):
     result = TimedeltaArrayMixin._evaluate_with_timedelta_like(
         self, other, op)
     if result is NotImplemented:
         return NotImplemented
     return Index(result, name=self.name, copy=False)
Пример #7
0
 def total_seconds(self):
     result = TimedeltaArrayMixin.total_seconds(self)
     return Index(result, name=self.name)
Пример #8
0
 def _evaluate_with_timedelta_like(self, other, op):
     result = TimedeltaArrayMixin._evaluate_with_timedelta_like(self, other,
                                                                op)
     if result is NotImplemented:
         return NotImplemented
     return Index(result, name=self.name, copy=False)
Пример #9
0
 def test_from_tdi(self):
     tdi = pd.TimedeltaIndex(['1 Day', '3 Hours'])
     arr = TimedeltaArrayMixin(tdi)
     assert list(arr) == list(tdi)