Apply a function to each block of this Dataset. .. warning:: This method is experimental and its signature may change. Parameters ---------- func : callable User-provided function that accepts a Dataset as its first param
(
self,
func: Callable[..., T_Xarray],
args: Sequence[Any] = (),
kwargs: Mapping[str, Any] | None = None,
template: DataArray | Dataset | None = None,
)
| 8815 | return unify_chunks(self)[0] |
| 8816 | |
| 8817 | def map_blocks( |
| 8818 | self, |
| 8819 | func: Callable[..., T_Xarray], |
| 8820 | args: Sequence[Any] = (), |
| 8821 | kwargs: Mapping[str, Any] | None = None, |
| 8822 | template: DataArray | Dataset | None = None, |
| 8823 | ) -> T_Xarray: |
| 8824 | """ |
| 8825 | Apply a function to each block of this Dataset. |
| 8826 | |
| 8827 | .. warning:: |
| 8828 | This method is experimental and its signature may change. |
| 8829 | |
| 8830 | Parameters |
| 8831 | ---------- |
| 8832 | func : callable |
| 8833 | User-provided function that accepts a Dataset as its first |
| 8834 | parameter. The function will receive a subset or 'block' of this Dataset (see below), |
| 8835 | corresponding to one chunk along each chunked dimension. ``func`` will be |
| 8836 | executed as ``func(subset_dataset, *subset_args, **kwargs)``. |
| 8837 | |
| 8838 | This function must return either a single DataArray or a single Dataset. |
| 8839 | |
| 8840 | This function cannot add a new chunked dimension. |
| 8841 | args : sequence |
| 8842 | Passed to func after unpacking and subsetting any xarray objects by blocks. |
| 8843 | xarray objects in args must be aligned with obj, otherwise an error is raised. |
| 8844 | kwargs : Mapping or None |
| 8845 | Passed verbatim to func after unpacking. xarray objects, if any, will not be |
| 8846 | subset to blocks. Passing dask collections in kwargs is not allowed. |
| 8847 | template : DataArray, Dataset or None, optional |
| 8848 | xarray object representing the final result after compute is called. If not provided, |
| 8849 | the function will be first run on mocked-up data, that looks like this object but |
| 8850 | has sizes 0, to determine properties of the returned object such as dtype, |
| 8851 | variable names, attributes, new dimensions and new indexes (if any). |
| 8852 | ``template`` must be provided if the function changes the size of existing dimensions. |
| 8853 | When provided, ``attrs`` on variables in `template` are copied over to the result. Any |
| 8854 | ``attrs`` set by ``func`` will be ignored. |
| 8855 | |
| 8856 | Returns |
| 8857 | ------- |
| 8858 | A single DataArray or Dataset with dask backend, reassembled from the outputs of the |
| 8859 | function. |
| 8860 | |
| 8861 | Notes |
| 8862 | ----- |
| 8863 | This function is designed for when ``func`` needs to manipulate a whole xarray object |
| 8864 | subset to each block. Each block is loaded into memory. In the more common case where |
| 8865 | ``func`` can work on numpy arrays, it is recommended to use ``apply_ufunc``. |
| 8866 | |
| 8867 | If none of the variables in this object is backed by dask arrays, calling this function is |
| 8868 | equivalent to calling ``func(obj, *args, **kwargs)``. |
| 8869 | |
| 8870 | See Also |
| 8871 | -------- |
| 8872 | :func:`dask.array.map_blocks <dask.array.map_blocks>` |
| 8873 | :func:`xarray.apply_ufunc <apply_ufunc>` |
| 8874 | :func:`xarray.DataArray.map_blocks <xarray.DataArray.map_blocks>` |