xarray.Dataset.roll#
- Dataset.roll(shifts=None, roll_coords=False, **shifts_kwargs)[source]#
Roll this dataset by an offset along one or more dimensions.
Unlike shift, roll treats the given dimensions as periodic, so will not create any missing values to be filled.
Also unlike shift, roll may rotate all variables, including coordinates if specified. The direction of rotation is consistent with
numpy.roll()
.- Parameters
shifts (mapping of hashable to
int
, optional) – A dict with keys matching dimensions and values given by integers to rotate each of the given dimensions. Positive offsets roll to the right; negative offsets roll to the left.roll_coords (
bool
, default:False
) – Indicates whether to roll the coordinates by the offset too.**shifts_kwargs (
{dim: offset, ...}
, optional) – The keyword arguments form ofshifts
. One of shifts or shifts_kwargs must be provided.
- Returns
rolled (
Dataset
) – Dataset with the same attributes but rolled data and coordinates.
See also
Examples
>>> ds = xr.Dataset({"foo": ("x", list("abcde"))}, coords={"x": np.arange(5)}) >>> ds.roll(x=2) <xarray.Dataset> Size: 60B Dimensions: (x: 5) Coordinates: * x (x) int64 40B 0 1 2 3 4 Data variables: foo (x) <U1 20B 'd' 'e' 'a' 'b' 'c'
>>> ds.roll(x=2, roll_coords=True) <xarray.Dataset> Size: 60B Dimensions: (x: 5) Coordinates: * x (x) int64 40B 3 4 0 1 2 Data variables: foo (x) <U1 20B 'd' 'e' 'a' 'b' 'c'