Apply a function to each array in the group and concatenate them together into a new array. `func` is called like `func(ar, *args, **kwargs)` for each array `ar` in this group. Apply uses heuristics (like `pandas.GroupBy.apply`) to figure out how to stack to
(
self,
func: Callable[..., DataArray],
args: tuple[Any, ...] = (),
shortcut: bool | None = None,
**kwargs: Any,
)
| 1612 | return stacked |
| 1613 | |
| 1614 | def map( |
| 1615 | self, |
| 1616 | func: Callable[..., DataArray], |
| 1617 | args: tuple[Any, ...] = (), |
| 1618 | shortcut: bool | None = None, |
| 1619 | **kwargs: Any, |
| 1620 | ) -> DataArray: |
| 1621 | """Apply a function to each array in the group and concatenate them |
| 1622 | together into a new array. |
| 1623 | |
| 1624 | `func` is called like `func(ar, *args, **kwargs)` for each array `ar` |
| 1625 | in this group. |
| 1626 | |
| 1627 | Apply uses heuristics (like `pandas.GroupBy.apply`) to figure out how |
| 1628 | to stack together the array. The rule is: |
| 1629 | |
| 1630 | 1. If the dimension along which the group coordinate is defined is |
| 1631 | still in the first grouped array after applying `func`, then stack |
| 1632 | over this dimension. |
| 1633 | 2. Otherwise, stack over the new dimension given by name of this |
| 1634 | grouping (the argument to the `groupby` function). |
| 1635 | |
| 1636 | Parameters |
| 1637 | ---------- |
| 1638 | func : callable |
| 1639 | Callable to apply to each array. |
| 1640 | shortcut : bool, optional |
| 1641 | Whether or not to shortcut evaluation under the assumptions that: |
| 1642 | |
| 1643 | (1) The action of `func` does not depend on any of the array |
| 1644 | metadata (attributes or coordinates) but only on the data and |
| 1645 | dimensions. |
| 1646 | (2) The action of `func` creates arrays with homogeneous metadata, |
| 1647 | that is, with the same dimensions and attributes. |
| 1648 | |
| 1649 | If these conditions are satisfied `shortcut` provides significant |
| 1650 | speedup. This should be the case for many common groupby operations |
| 1651 | (e.g., applying numpy ufuncs). |
| 1652 | *args : tuple, optional |
| 1653 | Positional arguments passed to `func`. |
| 1654 | **kwargs |
| 1655 | Used to call `func(ar, **kwargs)` for each array `ar`. |
| 1656 | |
| 1657 | Returns |
| 1658 | ------- |
| 1659 | applied : DataArray |
| 1660 | The result of splitting, applying and combining this array. |
| 1661 | """ |
| 1662 | grouped = self._iter_grouped_shortcut() if shortcut else self._iter_grouped() |
| 1663 | applied = (maybe_wrap_array(arr, func(arr, *args, **kwargs)) for arr in grouped) |
| 1664 | return self._combine(applied, shortcut=shortcut) |
| 1665 | |
| 1666 | def apply(self, func, shortcut=False, args=(), **kwargs): |
| 1667 | """ |
no test coverage detected