MCPcopy Index your code
hub / github.com/pydata/xarray / reduce

Method reduce

xarray/namedarray/core.py:864–947  ·  view source on GitHub ↗

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

(
        self,
        func: Callable[..., Any],
        dim: Dims = None,
        axis: int | Sequence[int] | None = None,
        keepdims: bool = False,
        **kwargs: Any,
    )

Source from the content-addressed store, hash-verified

862 return self._replace(data=self.to_numpy())
863
864 def reduce(
865 self,
866 func: Callable[..., Any],
867 dim: Dims = None,
868 axis: int | Sequence[int] | None = None,
869 keepdims: bool = False,
870 **kwargs: Any,
871 ) -> NamedArray[Any, Any]:
872 """Reduce this array by applying `func` along some dimension(s).
873
874 Parameters
875 ----------
876 func : callable
877 Function which can be called in the form
878 `func(x, axis=axis, **kwargs)` to return the result of reducing an
879 np.ndarray over an integer valued axis.
880 dim : "...", str, Iterable of Hashable or None, optional
881 Dimension(s) over which to apply `func`. By default `func` is
882 applied over all dimensions.
883 axis : int or Sequence of int, optional
884 Axis(es) over which to apply `func`. Only one of the 'dim'
885 and 'axis' arguments can be supplied. If neither are supplied, then
886 the reduction is calculated over the flattened array (by calling
887 `func(x)` without an axis argument).
888 keepdims : bool, default: False
889 If True, the dimensions which are reduced are left in the result
890 as dimensions of size one
891 **kwargs : dict
892 Additional keyword arguments passed on to `func`.
893
894 Returns
895 -------
896 reduced : Array
897 Array with summarized data and the indicated dimension(s)
898 removed.
899 """
900 if dim == ...:
901 dim = None
902 if dim is not None and axis is not None:
903 raise ValueError("cannot supply both 'axis' and 'dim' arguments")
904
905 if dim is not None:
906 axis = self.get_axis_num(dim)
907
908 with warnings.catch_warnings():
909 warnings.filterwarnings(
910 "ignore", r"Mean of empty slice", category=RuntimeWarning
911 )
912 if axis is not None:
913 if isinstance(axis, tuple) and len(axis) == 1:
914 # unpack axis for the benefit of functions
915 # like np.argmin which can't handle tuple arguments
916 axis = axis[0]
917 data = func(self.data, axis=axis, **kwargs)
918 else:
919 data = func(self.data, **kwargs)
920
921 if getattr(data, "shape", ()) == self.shape:

Callers

nothing calls this directly

Calls 3

get_axis_numMethod · 0.95
from_arrayFunction · 0.85
funcFunction · 0.50

Tested by

no test coverage detected