Reduce this array by applying `func` along some dimension(s). Parameters ---------- func : callable Function which can be called in the form `func(x, axis=axis, **kwargs)` to return the result of reducing an np.ndarray over an integer valu
( # type: ignore[override]
self,
func: Callable[..., Any],
dim: Dims = None,
axis: int | Sequence[int] | None = None,
keep_attrs: bool | None = None,
keepdims: bool = False,
**kwargs,
)
| 1728 | return apply_ufunc(xp.clip, self, min, max, dask="allowed") |
| 1729 | |
| 1730 | def reduce( # type: ignore[override] |
| 1731 | self, |
| 1732 | func: Callable[..., Any], |
| 1733 | dim: Dims = None, |
| 1734 | axis: int | Sequence[int] | None = None, |
| 1735 | keep_attrs: bool | None = None, |
| 1736 | keepdims: bool = False, |
| 1737 | **kwargs, |
| 1738 | ) -> Variable: |
| 1739 | """Reduce this array by applying `func` along some dimension(s). |
| 1740 | |
| 1741 | Parameters |
| 1742 | ---------- |
| 1743 | func : callable |
| 1744 | Function which can be called in the form |
| 1745 | `func(x, axis=axis, **kwargs)` to return the result of reducing an |
| 1746 | np.ndarray over an integer valued axis. |
| 1747 | dim : "...", str, Iterable of Hashable or None, optional |
| 1748 | Dimension(s) over which to apply `func`. By default `func` is |
| 1749 | applied over all dimensions. |
| 1750 | axis : int or Sequence of int, optional |
| 1751 | Axis(es) over which to apply `func`. Only one of the 'dim' |
| 1752 | and 'axis' arguments can be supplied. If neither are supplied, then |
| 1753 | the reduction is calculated over the flattened array (by calling |
| 1754 | `func(x)` without an axis argument). |
| 1755 | keep_attrs : bool, optional |
| 1756 | If True (default), the variable's attributes (`attrs`) will be copied from |
| 1757 | the original object to the new one. If False, the new |
| 1758 | object will be returned without attributes. |
| 1759 | keepdims : bool, default: False |
| 1760 | If True, the dimensions which are reduced are left in the result |
| 1761 | as dimensions of size one |
| 1762 | **kwargs : dict |
| 1763 | Additional keyword arguments passed on to `func`. |
| 1764 | |
| 1765 | Returns |
| 1766 | ------- |
| 1767 | reduced : Array |
| 1768 | Array with summarized data and the indicated dimension(s) |
| 1769 | removed. |
| 1770 | """ |
| 1771 | keep_attrs_ = ( |
| 1772 | _get_keep_attrs(default=True) if keep_attrs is None else keep_attrs |
| 1773 | ) |
| 1774 | |
| 1775 | # Note that the call order for Variable.mean is |
| 1776 | # Variable.mean -> NamedArray.mean -> Variable.reduce |
| 1777 | # -> NamedArray.reduce |
| 1778 | result = super().reduce( |
| 1779 | func=func, dim=dim, axis=axis, keepdims=keepdims, **kwargs |
| 1780 | ) |
| 1781 | |
| 1782 | # return Variable always to support IndexVariable |
| 1783 | return Variable( |
| 1784 | result.dims, result._data, attrs=result._attrs if keep_attrs_ else None |
| 1785 | ) |
| 1786 | |
| 1787 | @classmethod |