xarray.DataArray.resample¶
-
DataArray.
resample
(self, indexer: Mapping[Hashable, str] = None, skipna=None, closed: str = None, label: str = None, base: int = 0, keep_attrs: bool = None, loffset=None, restore_coord_dims: bool = None, **indexer_kwargs: str)¶ Returns a Resample object for performing resampling operations.
Handles both downsampling and upsampling. If any intervals contain no values from the original object, they will be given the value
NaN
.- Parameters
indexer ({dim: freq}, optional) – Mapping from the dimension name to resample frequency.
skipna (bool, optional) – Whether to skip missing values when aggregating in downsampling.
closed ('left' or 'right', optional) – Side of each interval to treat as closed.
label ('left or '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.
loffset (timedelta or str, optional) – Offset used to adjust the resampled time labels. Some pandas date offset strings are supported.
keep_attrs (bool, optional) – If True, the object’s attributes (attrs) will be copied from the original object to the new one. If False (default), the new object will be returned without attributes.
restore_coord_dims (bool, optional) – If True, also restore the dimension order of multi-dimensional coordinates.
**indexer_kwargs ({dim: freq}) – The keyword arguments form of
indexer
. One of indexer or indexer_kwargs must be provided.
- Returns
resampled – This object resampled.
- Return type
same type as caller
Examples
Downsample monthly time-series data to seasonal data:
>>> da = xr.DataArray(np.linspace(0, 11, num=12), ... coords=[pd.date_range('15/12/1999', ... 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-02-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') <xarray.DataArray (time: 337)> array([ 0. , 0.032258, 0.064516, ..., 10.935484, 10.967742, 11. ]) Coordinates: * time (time) datetime64[ns] 1999-12-15 1999-12-16 1999-12-17 ...
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