xarray.map_over_datasets

xarray.map_over_datasets#

xarray.map_over_datasets(func, *args)[source]#

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:

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()) – Function to apply to datasets with signature:

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

    (i.e. func must accept at least one Dataset and return at least one Dataset.)

  • *args (tuple, optional) – Positional arguments passed on to func. Any DataTree arguments will be converted to Dataset objects via .dataset.

Returns

  • Result of applying `func` to each node in the provided trees, packed back

  • into DataTree objects via `DataTree.from_dict.`