apply_ufunc

Xarray method that applies custom function on underlying array in xarray. The underlying array can be a numpy or a dask array (need verification).

How it works?

  1. When xarray DataArray is given to apply_ufunc, it gets the underlying array, and passes to the custom function. Custom function returns the result array which again wrapped by apply_ufunc and returned to us.
  2. When xarray DataSet is given, it loops over the data variables(DataArray), passes each of the data array to the custom function, gets the result array and wraps in Data Array and then in Data set and returns.
  3. All the input DataSet/DataArray metadata gets populated on the result by apply_ufunc.
  4. By default, attributes are not populated on the result but can retained using keep_attrs=True.

Note

vectorize=True doesn’t work when input array is a dask array.

Syntax

It is the minimal syntax

xr.apply_ufunct(custom_function, dataset/dataarray, pos_args, kwargs={}, keep_attrs=True)

input_core_dims

  • This parameter takes the dimensions to which the function would be operated on.
  • specified dimensions are moved to last. for example, input_core_dims=["time"]("time") will be moved to last dimension.

References

  1. https://tutorial.xarray.dev/advanced/apply_ufunc/simple_numpy_apply_ufunc.html