🍾 Xarray is now 10 years old! 🎉

xarray.core.accessor_dt.TimedeltaAccessor

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)> Size: 160B
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] 160B 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)> Size: 160B
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] 160B 1 days 00:00:00 ... 5 days 18:00:00
>>> ts.dt.microseconds
<xarray.DataArray 'microseconds' (time: 20)> Size: 160B
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] 160B 1 days 00:00:00 ... 5 days 18:00:00
>>> ts.dt.seconds
<xarray.DataArray 'seconds' (time: 20)> Size: 160B
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] 160B 1 days 00:00:00 ... 5 days 18:00:00
>>> ts.dt.total_seconds()
<xarray.DataArray 'total_seconds' (time: 20)> Size: 160B
array([ 86400., 108000., 129600., 151200., 172800., 194400., 216000.,
       237600., 259200., 280800., 302400., 324000., 345600., 367200.,
       388800., 410400., 432000., 453600., 475200., 496800.])
Coordinates:
  * time     (time) timedelta64[ns] 160B 1 days 00:00:00 ... 5 days 18:00:00
__init__(obj)[source]#

Methods

__init__(obj)

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.

total_seconds()

Total duration of each element expressed in seconds.

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