🍾 Xarray is now 10 years old! 🎉

xarray.Dataset.thin

Contents

xarray.Dataset.thin#

Dataset.thin(indexers=None, **indexers_kwargs)[source]#

Returns a new dataset with each array indexed along every n-th value for the specified dimension(s)

Parameters:
  • indexers (dict or int) – A dict with keys matching dimensions and integer values n or a single integer n applied over all dimensions. One of indexers or indexers_kwargs must be provided.

  • **indexers_kwargs ({dim: n, ...}, optional) – The keyword arguments form of indexers. One of indexers or indexers_kwargs must be provided.

Examples

>>> x_arr = np.arange(0, 26)
>>> x_arr
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25])
>>> x = xr.DataArray(
...     np.reshape(x_arr, (2, 13)),
...     dims=("x", "y"),
...     coords={"x": [0, 1], "y": np.arange(0, 13)},
... )
>>> x_ds = xr.Dataset({"foo": x})
>>> x_ds
<xarray.Dataset> Size: 328B
Dimensions:  (x: 2, y: 13)
Coordinates:
  * x        (x) int64 16B 0 1
  * y        (y) int64 104B 0 1 2 3 4 5 6 7 8 9 10 11 12
Data variables:
    foo      (x, y) int64 208B 0 1 2 3 4 5 6 7 8 ... 17 18 19 20 21 22 23 24 25
>>> x_ds.thin(3)
<xarray.Dataset> Size: 88B
Dimensions:  (x: 1, y: 5)
Coordinates:
  * x        (x) int64 8B 0
  * y        (y) int64 40B 0 3 6 9 12
Data variables:
    foo      (x, y) int64 40B 0 3 6 9 12
>>> x.thin({"x": 2, "y": 5})
<xarray.DataArray (x: 1, y: 3)> Size: 24B
array([[ 0,  5, 10]])
Coordinates:
  * x        (x) int64 8B 0
  * y        (y) int64 24B 0 5 10