xarray.map_blocks

xarray.map_blocks(func: Callable[..., ~T_DSorDA], obj: Union[xarray.core.dataarray.DataArray, xarray.core.dataset.Dataset], args: Sequence[Any] = (), kwargs: Mapping[str, Any] = None) → ~T_DSorDA

Apply a function to each chunk of a DataArray or Dataset. This function is experimental and its signature may change.

Parameters
  • func (callable) –

    User-provided function that accepts a DataArray or Dataset as its first parameter. The function will receive a subset of ‘obj’ (see below), corresponding to one chunk along each chunked dimension. func will be executed as func(obj_subset, *args, **kwargs).

    The function will be first run on mocked-up data, that looks like ‘obj’ but has sizes 0, to determine properties of the returned object such as dtype, variable names, new dimensions and new indexes (if any).

    This function must return either a single DataArray or a single Dataset.

    This function cannot change size of existing dimensions, or add new chunked dimensions.

  • obj (DataArray, Dataset) – Passed to the function as its first argument, one dask chunk at a time.

  • args (Sequence) – Passed verbatim to func after unpacking, after the sliced obj. xarray objects, if any, will not be split by chunks. Passing dask collections is not allowed.

  • kwargs (Mapping) – Passed verbatim to func after unpacking. xarray objects, if any, will not be split by chunks. Passing dask collections is not allowed.

Returns

  • A single DataArray or Dataset with dask backend, reassembled from the outputs of the

  • function.

Notes

This function is designed for when one needs to manipulate a whole xarray object within each chunk. In the more common case where one can work on numpy arrays, it is recommended to use apply_ufunc.

If none of the variables in obj is backed by dask, calling this function is equivalent to calling func(obj, *args, **kwargs).