xarray.core.accessor_dt.DatetimeAccessor¶
-
class
xarray.core.accessor_dt.DatetimeAccessor(obj)¶ Access datetime fields for DataArrays with datetime-like dtypes.
Fields can be accessed through the .dt attribute for applicable DataArrays.
Notes
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.
Examples
>>> import xarray as xr >>> import pandas as pd >>> dates = pd.date_range(start='2000/01/01', freq='D', periods=10) >>> ts = xr.DataArray(dates, dims=('time')) >>> ts <xarray.DataArray (time: 10)> array(['2000-01-01T00:00:00.000000000', '2000-01-02T00:00:00.000000000', '2000-01-03T00:00:00.000000000', '2000-01-04T00:00:00.000000000', '2000-01-05T00:00:00.000000000', '2000-01-06T00:00:00.000000000', '2000-01-07T00:00:00.000000000', '2000-01-08T00:00:00.000000000', '2000-01-09T00:00:00.000000000', '2000-01-10T00:00:00.000000000'], dtype='datetime64[ns]') Coordinates: * time (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2000-01-10 >>> ts.dt <xarray.core.accessor_dt.DatetimeAccessor object at 0x118b54d68> >>> ts.dt.dayofyear <xarray.DataArray 'dayofyear' (time: 10)> array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) Coordinates: * time (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2000-01-10 >>> ts.dt.quarter <xarray.DataArray 'quarter' (time: 10)> array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) Coordinates: * time (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2000-01-10
-
__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
dayThe days of the datetime dayofweekThe day of the week with Monday=0, Sunday=6 dayofyearThe ordinal day of the year days_in_monthThe number of days in the month daysinmonthThe number of days in the month hourThe hours of the datetime is_leap_yearBoolean indicator if the date belongs to a leap year. is_month_endIndicates whether the date is the last day of the month. is_month_startIndicates whether the date is the first day of the month. is_quarter_endIndicator for whether the date is the last day of a quarter. is_quarter_startIndicator for whether the date is the first day of a quarter. is_year_endIndicate whether the date is the last day of the year. is_year_startIndicate whether the date is the first day of a year. microsecondThe microseconds of the datetime minuteThe minutes of the datetime monthThe month as January=1, December=12 nanosecondThe nanoseconds of the datetime quarterThe quarter of the date seasonSeason of the year secondThe seconds of the datetime timeTimestamps corresponding to datetimes weekThe week ordinal of the year weekdayThe day of the week with Monday=0, Sunday=6 weekday_nameThe name of day in a week weekofyearThe week ordinal of the year yearThe year of the datetime -