Apply a function to each block of a DataArray or Dataset. .. warning:: 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 ``obj``
(
func: Callable[..., T_Xarray],
obj: DataArray | Dataset,
args: Sequence[Any] = (),
kwargs: Mapping[str, Any] | None = None,
template: DataArray | Dataset | None = None,
)
| 226 | |
| 227 | |
| 228 | def map_blocks( |
| 229 | func: Callable[..., T_Xarray], |
| 230 | obj: DataArray | Dataset, |
| 231 | args: Sequence[Any] = (), |
| 232 | kwargs: Mapping[str, Any] | None = None, |
| 233 | template: DataArray | Dataset | None = None, |
| 234 | ) -> T_Xarray: |
| 235 | """Apply a function to each block of a DataArray or Dataset. |
| 236 | |
| 237 | .. warning:: |
| 238 | This function is experimental and its signature may change. |
| 239 | |
| 240 | Parameters |
| 241 | ---------- |
| 242 | func : callable |
| 243 | User-provided function that accepts a DataArray or Dataset as its first |
| 244 | parameter ``obj``. The function will receive a subset or 'block' of ``obj`` (see below), |
| 245 | corresponding to one chunk along each chunked dimension. ``func`` will be |
| 246 | executed as ``func(subset_obj, *subset_args, **kwargs)``. |
| 247 | |
| 248 | This function must return either a single DataArray or a single Dataset. |
| 249 | |
| 250 | This function cannot add a new chunked dimension. |
| 251 | obj : DataArray, Dataset |
| 252 | Passed to the function as its first argument, one block at a time. |
| 253 | args : sequence |
| 254 | Passed to func after unpacking and subsetting any xarray objects by blocks. |
| 255 | xarray objects in args must be aligned with obj, otherwise an error is raised. |
| 256 | kwargs : mapping |
| 257 | Passed verbatim to func after unpacking. xarray objects, if any, will not be |
| 258 | subset to blocks. Passing dask collections in kwargs is not allowed. |
| 259 | template : DataArray or Dataset, optional |
| 260 | xarray object representing the final result after compute is called. If not provided, |
| 261 | the function will be first run on mocked-up data, that looks like ``obj`` but |
| 262 | has sizes 0, to determine properties of the returned object such as dtype, |
| 263 | variable names, attributes, new dimensions and new indexes (if any). |
| 264 | ``template`` must be provided if the function changes the size of existing dimensions. |
| 265 | When provided, ``attrs`` on variables in `template` are copied over to the result. Any |
| 266 | ``attrs`` set by ``func`` will be ignored. |
| 267 | |
| 268 | Returns |
| 269 | ------- |
| 270 | obj : same as obj |
| 271 | A single DataArray or Dataset with dask backend, reassembled from the outputs of the |
| 272 | function. |
| 273 | |
| 274 | Notes |
| 275 | ----- |
| 276 | This function is designed for when ``func`` needs to manipulate a whole xarray object |
| 277 | subset to each block. Each block is loaded into memory. In the more common case where |
| 278 | ``func`` can work on numpy arrays, it is recommended to use ``apply_ufunc``. |
| 279 | |
| 280 | If none of the variables in ``obj`` is backed by dask arrays, calling this function is |
| 281 | equivalent to calling ``func(obj, *args, **kwargs)``. |
| 282 | |
| 283 | See Also |
| 284 | -------- |
| 285 | dask.array.map_blocks, xarray.apply_ufunc, xarray.Dataset.map_blocks |
no test coverage detected
searching dependent graphs…