xarray.core.accessor_dt.TimedeltaAccessor

class xarray.core.accessor_dt.TimedeltaAccessor(obj)[source]

Access Timedelta fields for DataArrays with Timedelta-like dtypes.

Fields can be accessed through the .dt attribute for applicable DataArrays.

Examples

>>> dates = pd.timedelta_range(start="1 day", freq="6H", periods=20)
>>> ts = xr.DataArray(dates, dims=("time"))
>>> ts
<xarray.DataArray (time: 20)>
array([ 86400000000000, 108000000000000, 129600000000000, 151200000000000,
       172800000000000, 194400000000000, 216000000000000, 237600000000000,
       259200000000000, 280800000000000, 302400000000000, 324000000000000,
       345600000000000, 367200000000000, 388800000000000, 410400000000000,
       432000000000000, 453600000000000, 475200000000000, 496800000000000],
      dtype='timedelta64[ns]')
Coordinates:
  * time     (time) timedelta64[ns] 1 days 00:00:00 ... 5 days 18:00:00
>>> ts.dt  
<xarray.core.accessor_dt.TimedeltaAccessor object at 0x...>
>>> ts.dt.days
<xarray.DataArray 'days' (time: 20)>
array([1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5])
Coordinates:
  * time     (time) timedelta64[ns] 1 days 00:00:00 ... 5 days 18:00:00
>>> ts.dt.microseconds
<xarray.DataArray 'microseconds' (time: 20)>
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
Coordinates:
  * time     (time) timedelta64[ns] 1 days 00:00:00 ... 5 days 18:00:00
>>> ts.dt.seconds
<xarray.DataArray 'seconds' (time: 20)>
array([    0, 21600, 43200, 64800,     0, 21600, 43200, 64800,     0,
       21600, 43200, 64800,     0, 21600, 43200, 64800,     0, 21600,
       43200, 64800])
Coordinates:
  * time     (time) timedelta64[ns] 1 days 00:00:00 ... 5 days 18:00:00
__init__(obj)[source]

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__(obj)

Initialize self.

ceil(freq)

Round timestamps upward to specified frequency resolution.

floor(freq)

Round timestamps downward to specified frequency resolution.

round(freq)

Round timestamps to specified frequency resolution.

Attributes

days

Number of days for each element.

microseconds

Number of microseconds (>= 0 and less than 1 second) for each element.

nanoseconds

Number of nanoseconds (>= 0 and less than 1 microsecond) for each element.

seconds

Number of seconds (>= 0 and less than 1 day) for each element.