xarray.DataArray.map_blocks

DataArray.map_blocks(func, args=(), kwargs=None, template=None)

Apply a function to each block of this DataArray.

Warning

This method is experimental and its signature may change.

Parameters
  • func (callable) –

    User-provided function that accepts a DataArray as its first parameter. The function will receive a subset, i.e. one block, of this DataArray (see below), corresponding to one chunk along each chunked dimension. func will be executed as func(block_subset, *args, **kwargs).

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

    This function cannot add a new chunked dimension.

  • args (Sequence) – Passed verbatim to func after unpacking, after the sliced DataArray. 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.

  • template ((optional) DataArray, Dataset) – xarray object representing the final result after compute is called. If not provided, 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). ‘template’ must be provided if the function changes the size of existing dimensions.

Returns

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

  • the function.

Notes

This method 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 this DataArray is backed by dask, calling this method is equivalent to calling func(self, *args, **kwargs).