xarray.core.accessor_dt.DatetimeAccessor

class xarray.core.accessor_dt.DatetimeAccessor(obj)

Access datetime fields for DataArrays with datetime-like dtypes.

Similar to pandas, fields can be accessed through the .dt attribute for applicable DataArrays:

>>> ds = xarray.Dataset({'time': pd.date_range(start='2000/01/01',
...                                            freq='D', periods=100)})
>>> ds.time.dt
<xarray.core.accessors.DatetimeAccessor at 0x10c369f60>
>>> ds.time.dt.dayofyear[:5]
<xarray.DataArray 'dayofyear' (time: 5)>
array([1, 2, 3, 4, 5], dtype=int32)
Coordinates:
  * time     (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 ...

All of the pandas fields are accessible here. Note that these fields are not calendar-aware; if your datetimes are encoded with a non-Gregorian calendar (e.g. a 360-day calendar) using cftime, then some fields like dayofyear may not be accurate.

__init__(obj)

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.
strftime(date_format) Return an array of formatted strings specified by date_format, which supports the same string format as the python standard library.

Attributes

day The days of the datetime
dayofweek The day of the week with Monday=0, Sunday=6
dayofyear The ordinal day of the year
days_in_month The number of days in the month
daysinmonth The number of days in the month
hour The hours of the datetime
microsecond The microseconds of the datetime
minute The minutes of the datetime
month The month as January=1, December=12
nanosecond The nanoseconds of the datetime
quarter The quarter of the date
season DJF)
second The seconds of the datetime
time Timestamps corresponding to datetimes
week The week ordinal of the year
weekday The day of the week with Monday=0, Sunday=6
weekday_name Friday)
weekofyear The week ordinal of the year
year The year of the datetime