xarray.DataArray.resample#
- DataArray.resample(indexer=None, skipna=None, closed=None, label=None, base=None, offset=None, origin='start_day', keep_attrs=None, loffset=None, restore_coord_dims=None, **indexer_kwargs)[source]#
Returns a Resample object for performing resampling operations.
Handles both downsampling and upsampling. The resampled dimension must be a datetime-like coordinate. If any intervals contain no values from the original object, they will be given the value
NaN
.- Parameters:
indexer (
Mapping
ofHashable
tostr
, optional) – Mapping from the dimension name to resample frequency [1]. The dimension must be datetime-like.skipna (
bool
, optional) – Whether to skip missing values when aggregating in downsampling.closed (
{"left", "right"}
, optional) – Side of each interval to treat as closed.label (
{"left", "right"}
, optional) – Side of each interval to use for labeling.base (
int
, optional) – For frequencies that evenly subdivide 1 day, the “origin” of the aggregated intervals. For example, for “24H” frequency, base could range from 0 through 23.origin (
{'epoch', 'start', 'start_day', 'end', 'end_day'}
,pd.Timestamp
,datetime.datetime
,np.datetime64
, orcftime.datetime
, default'start_day'
) – The datetime on which to adjust the grouping. The timezone of origin must match the timezone of the index.If a datetime is not used, these values are also supported: - ‘epoch’: origin is 1970-01-01 - ‘start’: origin is the first value of the timeseries - ‘start_day’: origin is the first day at midnight of the timeseries - ‘end’: origin is the last value of the timeseries - ‘end_day’: origin is the ceiling midnight of the last day
offset (
pd.Timedelta
,datetime.timedelta
, orstr
, defaultis None
) – An offset timedelta added to the origin.loffset (
timedelta
orstr
, optional) – Offset used to adjust the resampled time labels. Some pandas date offset strings are supported.restore_coord_dims (
bool
, optional) – If True, also restore the dimension order of multi-dimensional coordinates.**indexer_kwargs (
str
) – The keyword arguments form ofindexer
. One of indexer or indexer_kwargs must be provided.
- Returns:
resampled (
core.resample.DataArrayResample
) – This object resampled.
Examples
Downsample monthly time-series data to seasonal data:
>>> da = xr.DataArray( ... np.linspace(0, 11, num=12), ... coords=[ ... pd.date_range( ... "1999-12-15", ... periods=12, ... freq=pd.DateOffset(months=1), ... ) ... ], ... dims="time", ... ) >>> da <xarray.DataArray (time: 12)> array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.]) Coordinates: * time (time) datetime64[ns] 1999-12-15 2000-01-15 ... 2000-11-15 >>> da.resample(time="QS-DEC").mean() <xarray.DataArray (time: 4)> array([ 1., 4., 7., 10.]) Coordinates: * time (time) datetime64[ns] 1999-12-01 2000-03-01 2000-06-01 2000-09-01
Upsample monthly time-series data to daily data:
>>> da.resample(time="1D").interpolate("linear") # +doctest: ELLIPSIS <xarray.DataArray (time: 337)> array([ 0. , 0.03225806, 0.06451613, 0.09677419, 0.12903226, 0.16129032, 0.19354839, 0.22580645, 0.25806452, 0.29032258, 0.32258065, 0.35483871, 0.38709677, 0.41935484, 0.4516129 , ... 10.80645161, 10.83870968, 10.87096774, 10.90322581, 10.93548387, 10.96774194, 11. ]) Coordinates: * time (time) datetime64[ns] 1999-12-15 1999-12-16 ... 2000-11-15
Limit scope of upsampling method
>>> da.resample(time="1D").nearest(tolerance="1D") <xarray.DataArray (time: 337)> array([ 0., 0., nan, ..., nan, 11., 11.]) Coordinates: * time (time) datetime64[ns] 1999-12-15 1999-12-16 ... 2000-11-15
References