xarray.Dataset.apply

Dataset.apply(func, keep_attrs=None, args=(), **kwargs)

Apply a function over the data variables in this dataset.

Parameters
funcfunction

Function which can be called in the form func(x, *args, **kwargs) to transform each DataArray x in this dataset into another DataArray.

keep_attrsbool, optional

If True, the dataset’s attributes (attrs) will be copied from the original object to the new one. If False, the new object will be returned without attributes.

argstuple, optional

Positional arguments passed on to func.

**kwargsdict

Keyword arguments passed on to func.

Returns
appliedDataset

Resulting dataset from applying func over each data variable.

Examples

>>> da = xr.DataArray(np.random.randn(2, 3))
>>> ds = xr.Dataset({'foo': da, 'bar': ('x', [-1, 2])})
>>> ds
<xarray.Dataset>
Dimensions:  (dim_0: 2, dim_1: 3, x: 2)
Dimensions without coordinates: dim_0, dim_1, x
Data variables:
    foo      (dim_0, dim_1) float64 -0.3751 -1.951 -1.945 0.2948 0.711 -0.3948
    bar      (x) int64 -1 2
>>> ds.apply(np.fabs)
<xarray.Dataset>
Dimensions:  (dim_0: 2, dim_1: 3, x: 2)
Dimensions without coordinates: dim_0, dim_1, x
Data variables:
    foo      (dim_0, dim_1) float64 0.3751 1.951 1.945 0.2948 0.711 0.3948
    bar      (x) float64 1.0 2.0