# xarray.map_over_datasets

### xarray.map_over_datasets(func, \*args, kwargs=None)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/datatree_mapping.py#L41-L138)

Applies a function to every dataset in one or more DataTree objects with
the same structure (ie.., that are isomorphic), returning new trees which
store the results.

The function will be applied to any dataset stored in any of the nodes in
the trees. The returned trees will have the same structure as the supplied
trees.

`func` needs to return a Dataset, tuple of Dataset objects or None in order
to be able to rebuild the subtrees after mapping, as each result will be
assigned to its respective node of a new tree via DataTree.from_dict. Any
returned value that is one of these types will be stacked into a separate
tree before returning all of them.

`map_over_datasets` is essentially syntactic sugar for the combination of
`group_subtrees` and `DataTree.from_dict`. For example, in the case of
a two argument function that return one result, it is equivalent to:

```default
results = {}
for path, (left, right) in group_subtrees(left_tree, right_tree):
    results[path] = func(left.dataset, right.dataset)
return DataTree.from_dict(results)
```

* **Parameters:**
  * **func** ([`callable()`](https://docs.python.org/3/builtins/functions.html#callable)) – Function to apply to datasets with signature:

    func(\*args: Dataset, \*\*kwargs) -> Union[Dataset, tuple[Dataset, …]].

    (i.e. func must accept at least one Dataset and return at least one Dataset.)
  * **\*args** ([`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple), *optional*) – Positional arguments passed on to func. Any DataTree arguments will be
    converted to Dataset objects via .dataset.
  * **kwargs** ([`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict), *optional*) – Optional keyword arguments passed directly to `func`.
* **Returns:**
  * `Result` of `applying `func`` to `each node in the provided trees`, `packed back`
  * `into DataTree objects via `DataTree.from_dict`.\`

#### SEE ALSO
[`DataTree.map_over_datasets`](xarray.DataTree.map_over_datasets.html.md#xarray.DataTree.map_over_datasets), [`group_subtrees`](xarray.group_subtrees.html.md#xarray.group_subtrees), [`DataTree.from_dict`](xarray.DataTree.from_dict.html.md#xarray.DataTree.from_dict)
