From 6b9a81a6fbe3ba460d905f0e92105d8e25af3ebb Mon Sep 17 00:00:00 2001 From: dcherian Date: Sun, 7 Nov 2021 20:35:52 -0700 Subject: [PATCH 01/31] Better generator for reductions. --- xarray/core/_reductions.py | 158 ++++------ xarray/util/generate_reductions.py | 443 ++++++++++++++++------------- 2 files changed, 303 insertions(+), 298 deletions(-) diff --git a/xarray/core/_reductions.py b/xarray/core/_reductions.py index 67fbbd482d0..a2108f54a64 100644 --- a/xarray/core/_reductions.py +++ b/xarray/core/_reductions.py @@ -26,6 +26,19 @@ def reduce( ... +class DataArrayReduce(Protocol): + def reduce( + self, + func: Callable[..., Any], + dim: Union[None, Hashable, Sequence[Hashable]] = None, + axis: Union[None, int, Sequence[int]] = None, + keep_attrs: bool = None, + keepdims: bool = False, + **kwargs: Any, + ) -> T_DataArray: + ... + + class DatasetGroupByReductions: __slots__ = () @@ -42,8 +55,7 @@ def count( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -114,8 +126,7 @@ def all( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -186,8 +197,7 @@ def any( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -259,8 +269,7 @@ def max( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -348,8 +357,7 @@ def min( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -437,8 +445,7 @@ def mean( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -527,8 +534,7 @@ def prod( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -634,8 +640,7 @@ def sum( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -740,8 +745,7 @@ def std( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -829,8 +833,7 @@ def var( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -918,8 +921,7 @@ def median( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -1010,8 +1012,7 @@ def count( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -1082,8 +1083,7 @@ def all( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -1154,8 +1154,7 @@ def any( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -1227,8 +1226,7 @@ def max( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -1316,8 +1314,7 @@ def min( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -1405,8 +1402,7 @@ def mean( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -1495,8 +1491,7 @@ def prod( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -1602,8 +1597,7 @@ def sum( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -1708,8 +1702,7 @@ def std( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -1797,8 +1790,7 @@ def var( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -1886,8 +1878,7 @@ def median( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -1962,19 +1953,6 @@ def median( ) -class DataArrayReduce(Protocol): - def reduce( - self, - func: Callable[..., Any], - dim: Union[None, Hashable, Sequence[Hashable]] = None, - axis: Union[None, int, Sequence[int]] = None, - keep_attrs: bool = None, - keepdims: bool = False, - **kwargs: Any, - ) -> T_DataArray: - ... - - class DataArrayGroupByReductions: __slots__ = () @@ -1991,8 +1969,7 @@ def count( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -2057,8 +2034,7 @@ def all( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -2123,8 +2099,7 @@ def any( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -2190,8 +2165,7 @@ def max( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -2271,8 +2245,7 @@ def min( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -2352,8 +2325,7 @@ def mean( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -2434,8 +2406,7 @@ def prod( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -2531,8 +2502,7 @@ def sum( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -2627,8 +2597,7 @@ def std( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -2708,8 +2677,7 @@ def var( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -2789,8 +2757,7 @@ def median( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -2873,8 +2840,7 @@ def count( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -2939,8 +2905,7 @@ def all( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -3005,8 +2970,7 @@ def any( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -3072,8 +3036,7 @@ def max( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -3153,8 +3116,7 @@ def min( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -3234,8 +3196,7 @@ def mean( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -3316,8 +3277,7 @@ def prod( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -3413,8 +3373,7 @@ def sum( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -3509,8 +3468,7 @@ def std( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -3590,8 +3548,7 @@ def var( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not @@ -3671,8 +3628,7 @@ def median( ---------- dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, optional If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index 72449195d1e..fbda0b6d6de 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -15,8 +15,6 @@ import collections import textwrap -from functools import partial -from typing import Callable, Optional MODULE_PREAMBLE = '''\ """Mixin classes with reduction operations.""" @@ -53,50 +51,10 @@ def reduce( class {obj}{cls}Reductions: __slots__ = ()""" -_SKIPNA_DOCSTRING = """ -skipna : bool, optional - If True, skip missing values (as marked by NaN). By default, only - skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been - implemented (object, datetime64 or timedelta64).""" - -_MINCOUNT_DOCSTRING = """ -min_count : int, default: None - The required number of valid values to perform the operation. If - fewer than min_count non-NA values are present the result will be - NA. Only used if skipna is set to True or defaults to True for the - array's dtype. Changed in version 0.17.0: if specified on an integer - array and skipna=True, the result will be a float array.""" - - -BOOL_REDUCE_METHODS = ["all", "any"] -NAN_REDUCE_METHODS = [ - "max", - "min", - "mean", - "prod", - "sum", - "std", - "var", - "median", -] -NAN_CUM_METHODS = ["cumsum", "cumprod"] -MIN_COUNT_METHODS = ["prod", "sum"] -NUMERIC_ONLY_METHODS = [ - "mean", - "std", - "var", - "sum", - "prod", - "median", - "cumsum", - "cumprod", -] - -TEMPLATE_REDUCTION = ''' +TEMPLATE_REDUCTION_SIGNATURE = ''' def {method}( self: {obj}Reduce, - dim: Union[None, Hashable, Sequence[Hashable]] = None,{skip_na.kwarg}{min_count.kwarg} + dim: Union[None, Hashable, Sequence[Hashable]] = None,{extra_kwargs} keep_attrs: bool = None, **kwargs, ) -> T_{obj}: @@ -104,193 +62,284 @@ def {method}( Reduce this {obj}'s data by applying ``{method}`` along some dimension(s). Parameters - ---------- - dim : hashable or iterable of hashable, optional - Name of dimension[s] along which to apply ``{method}``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. {extra_dim}{extra_args}{skip_na.docs}{min_count.docs} - keep_attrs : bool, optional - If True, ``attrs`` will be copied from the original - object to the new one. If False (default), the new object will be - returned without attributes. - **kwargs : dict - Additional keyword arguments passed on to the appropriate array - function for calculating ``{method}`` on this object's data. + ----------''' +TEMPLATE_RETURNS = """ Returns ------- reduced : {obj} New {obj} with ``{method}`` applied to its data and the - indicated dimension(s) removed - - Examples - --------{example} + indicated dimension(s) removed""" +TEMPLATE_SEE_ALSO = ''' See Also -------- numpy.{method} {obj}.{method} :ref:`{docref}` - User guide on {docref} operations. - """ - return self.reduce( - duck_array_ops.{array_method}, - dim=dim,{skip_na.call}{min_count.call}{numeric_only_call} - keep_attrs=keep_attrs, - **kwargs, - )''' + User guide on {docref_description}. + """''' +_DIM_DOCSTRING = """dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``{method}``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions.""" -def generate_groupby_example(obj: str, cls: str, method: str): - """Generate examples for method.""" - dx = "ds" if obj == "Dataset" else "da" - if cls == "Resample": - calculation = f'{dx}.resample(time="3M").{method}' - elif cls == "GroupBy": - calculation = f'{dx}.groupby("labels").{method}' - else: - raise ValueError +_SKIPNA_DOCSTRING = """skipna : bool, optional + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or skipna=True has not been + implemented (object, datetime64 or timedelta64).""" - if method in BOOL_REDUCE_METHODS: - np_array = """ - ... np.array([True, True, True, True, True, False], dtype=bool),""" +_MINCOUNT_DOCSTRING = """min_count : int, default: None + The required number of valid values to perform the operation. If + fewer than min_count non-NA values are present the result will be + NA. Only used if skipna is set to True or defaults to True for the + array's dtype. Changed in version 0.17.0: if specified on an integer + array and skipna=True, the result will be a float array.""" - else: - np_array = """ - ... np.array([1, 2, 3, 1, 2, np.nan]),""" +_KEEP_ATTRS_DOCSTRING = """keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes.""" - create_da = f""" - >>> da = xr.DataArray({np_array} - ... dims="time", - ... coords=dict( - ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), - ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), - ... ), - ... )""" +_KWARGS_DOCSTRING = """**kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``{method}`` on this object's data.""" - if obj == "Dataset": - maybe_dataset = """ - >>> ds = xr.Dataset(dict(da=da)) - >>> ds""" - else: - maybe_dataset = """ - >>> da""" +NAN_CUM_METHODS = ["cumsum", "cumprod"] - if method in NAN_REDUCE_METHODS: - maybe_skipna = f""" +NUMERIC_ONLY_METHODS = [ + "cumsum", + "cumprod", +] +extra_kwarg = collections.namedtuple("extra_kwarg", "docs kwarg call example") +skip_na = extra_kwarg( + docs=_SKIPNA_DOCSTRING, + kwarg="skipna: bool = True,", + call="skipna=skipna,", + example="""\n Use ``skipna`` to control whether NaNs are ignored. - >>> {calculation}(skipna=False)""" - else: - maybe_skipna = "" + >>> {calculation}(skipna=False)""", +) +min_count = extra_kwarg( + docs=_MINCOUNT_DOCSTRING, + kwarg="min_count: Optional[int] = None,", + call="min_count=min_count,", + example="""\n + Specify ``min_count`` for finer control over when NaNs are ignored. - if method in MIN_COUNT_METHODS: - maybe_mincount = f""" + >>> {calculation}(skipna=True, min_count=2)""", +) - Specify ``min_count`` for finer control over when NaNs are ignored. - >>> {calculation}(skipna=True, min_count=2)""" - else: - maybe_mincount = "" +class Method: + def __init__( + self, + name, + bool_reduce=False, + extra_kwargs=tuple(), + numeric_only=False, + ): + self.name = name + self.extra_kwargs = extra_kwargs + self.numeric_only = numeric_only + + if bool_reduce: + self.array_method = f"array_{name}" + self.np_example_array = """ + ... np.array([True, True, True, True, True, False], dtype=bool),""" - return f"""{create_da}{maybe_dataset} + else: + self.array_method = name + self.np_example_array = """ + ... np.array([1, 2, 3, 1, 2, np.nan]),""" - >>> {calculation}(){maybe_skipna}{maybe_mincount}""" +class DataStructure: + def __init__(self, name, docstring_create, example_var_name, numeric_only=False): + self.name = name + self.docstring_create = docstring_create + self.example_var_name = example_var_name + self.numeric_only = numeric_only -def generate_method( - obj: str, - docref: str, - method: str, - skipna: bool, - example_generator: Callable, - array_method: Optional[str] = None, -): - if not array_method: - array_method = method - if obj == "Dataset": - if method in NUMERIC_ONLY_METHODS: - numeric_only_call = "\n numeric_only=True," +class ClassReductionGenerator: + def __init__( + self, + cls, + datastructure, + methods, + docref, + docref_description, + example_call_preamble, + ): + self.datastructure = datastructure + self.cls = cls + self.methods = methods + self.docref = docref + self.docref_description = docref_description + self.example_call_preamble = example_call_preamble + self.preamble = CLASS_PREAMBLE.format(obj=datastructure.name, cls=cls) + + def generate_methods(self): + yield [self.preamble] + for method in self.methods: + yield self.generate_method(method) + + def generate_method(self, method): + template_kwargs = dict(obj=self.datastructure.name, method=method.name) + + if method.extra_kwargs: + extra_kwargs = "\n " + "\n ".join( + [kwarg.kwarg for kwarg in method.extra_kwargs if kwarg.kwarg] + ) else: - numeric_only_call = "\n numeric_only=False," - else: - numeric_only_call = "" - - kwarg = collections.namedtuple("kwarg", "docs kwarg call") - if skipna: - skip_na = kwarg( - docs=textwrap.indent(_SKIPNA_DOCSTRING, " "), - kwarg="\n skipna: bool = True,", - call="\n skipna=skipna,", - ) - else: - skip_na = kwarg(docs="", kwarg="", call="") - - if method in MIN_COUNT_METHODS: - min_count = kwarg( - docs=textwrap.indent(_MINCOUNT_DOCSTRING, " "), - kwarg="\n min_count: Optional[int] = None,", - call="\n min_count=min_count,", - ) - else: - min_count = kwarg(docs="", kwarg="", call="") - - return TEMPLATE_REDUCTION.format( - obj=obj, - docref=docref, - method=method, - array_method=array_method, - extra_dim="""If ``None``, will reduce over all dimensions - present in the grouped variable.""", - extra_args="", - skip_na=skip_na, - min_count=min_count, - numeric_only_call=numeric_only_call, - example=example_generator(obj=obj, method=method), - ) - - -def render(obj: str, cls: str, docref: str, example_generator: Callable): - yield CLASS_PREAMBLE.format(obj=obj, cls=cls) - yield generate_method( - obj, - method="count", - docref=docref, - skipna=False, - example_generator=example_generator, - ) - for method in BOOL_REDUCE_METHODS: - yield generate_method( - obj, - method=method, - docref=docref, - skipna=False, - array_method=f"array_{method}", - example_generator=example_generator, + extra_kwargs = "" + + yield TEMPLATE_REDUCTION_SIGNATURE.format( + **template_kwargs, + extra_kwargs=extra_kwargs, ) - for method in NAN_REDUCE_METHODS: - yield generate_method( - obj, - method=method, - docref=docref, - skipna=True, - example_generator=example_generator, + + for text in [ + _DIM_DOCSTRING.format(method=method.name), + *(kwarg.docs for kwarg in method.extra_kwargs if kwarg.docs), + _KEEP_ATTRS_DOCSTRING, + _KWARGS_DOCSTRING.format(method=method.name), + ]: + if text: + yield textwrap.indent(text, 8 * " ") + + yield TEMPLATE_RETURNS.format(**template_kwargs) + + yield textwrap.indent(self.generate_example(method=method), "") + + yield TEMPLATE_SEE_ALSO.format( + **template_kwargs, + docref=self.docref, + docref_description=self.docref_description, ) + yield self.generate_code(method) + + def generate_example(self, method): + create_da = f""" + >>> da = xr.DataArray({method.np_example_array} + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... )""" + + calculation = f"{self.datastructure.example_var_name}{self.example_call_preamble}.{method.name}" + if method.extra_kwargs: + extra_examples = "".join( + kwarg.example for kwarg in method.extra_kwargs if kwarg.example + ).format(calculation=calculation, method=method.name) + else: + extra_examples = "" + + return f""" + Examples + --------{create_da}{self.datastructure.docstring_create} + + >>> {calculation}(){extra_examples}""" + + def generate_code(self, method): + extra_kwargs = [kwarg.call for kwarg in method.extra_kwargs if kwarg.call] + + if self.datastructure.numeric_only: + extra_kwargs.append(f"numeric_only={method.numeric_only},") + + if extra_kwargs: + extra_kwargs = "\n " + "\n ".join(extra_kwargs) + else: + extra_kwargs = "" + return f""" return self.reduce( + duck_array_ops.{method.array_method}, + dim=dim,{extra_kwargs} + keep_attrs=keep_attrs, + **kwargs, + )""" + + +METHODS = ( + Method("count"), + Method("all", bool_reduce=True), + Method("any", bool_reduce=True), + Method("max", extra_kwargs=(skip_na,)), + Method("min", extra_kwargs=(skip_na,)), + Method("mean", extra_kwargs=(skip_na,), numeric_only=True), + Method("prod", extra_kwargs=(skip_na, min_count), numeric_only=True), + Method("sum", extra_kwargs=(skip_na, min_count), numeric_only=True), + Method("std", extra_kwargs=(skip_na,), numeric_only=True), + Method("var", extra_kwargs=(skip_na,), numeric_only=True), + Method("median", extra_kwargs=(skip_na,), numeric_only=True), +) + +DatasetObject = DataStructure( + name="Dataset", + docstring_create=""" + >>> ds = xr.Dataset(dict(da=da)) + >>> ds""", + example_var_name="ds", + numeric_only=True, +) +DataArrayObject = DataStructure( + name="DataArray", + docstring_create=""" + >>> da""", + example_var_name="da", + numeric_only=False, +) + +DataArrayGroupByGenerator = ClassReductionGenerator( + cls="GroupBy", + datastructure=DataArrayObject, + methods=METHODS, + docref="groupby", + docref_description="groupby operations", + example_call_preamble='.groupby("labels")', +) +DataArrayResampleGenerator = ClassReductionGenerator( + cls="Resample", + datastructure=DataArrayObject, + methods=METHODS, + docref="resampling", + docref_description="resampling operations", + example_call_preamble='.resample(time="3M")', +) +DatasetGroupByGenerator = ClassReductionGenerator( + cls="GroupBy", + datastructure=DatasetObject, + methods=METHODS, + docref="groupby", + docref_description="groupby operations", + example_call_preamble='.groupby("labels")', +) +DatasetResampleGenerator = ClassReductionGenerator( + cls="Resample", + datastructure=DatasetObject, + methods=METHODS, + docref="resampling", + docref_description="resampling operations", + example_call_preamble='.resample(time="3M")', +) + if __name__ == "__main__": print(MODULE_PREAMBLE) - for obj in ["Dataset", "DataArray"]: - print(OBJ_PREAMBLE.format(obj=obj)) - for cls, docref in ( - ("GroupBy", "groupby"), - ("Resample", "resampling"), - ): - for line in render( - obj=obj, - cls=cls, - docref=docref, - example_generator=partial(generate_groupby_example, cls=cls), - ): + print(OBJ_PREAMBLE.format(obj="Dataset")) + print(OBJ_PREAMBLE.format(obj="DataArray")) + for gen in [ + DatasetGroupByGenerator, + DatasetResampleGenerator, + DataArrayGroupByGenerator, + DataArrayResampleGenerator, + ]: + for lines in gen.generate_methods(): + for line in lines: print(line) From 569c67f28b3e7ff4c475793325a4388220932d02 Mon Sep 17 00:00:00 2001 From: dcherian Date: Sun, 7 Nov 2021 20:54:42 -0700 Subject: [PATCH 02/31] Add ddof for var, std --- xarray/core/_reductions.py | 112 +++++++++++++++++++++++++++++ xarray/util/generate_reductions.py | 17 ++++- 2 files changed, 127 insertions(+), 2 deletions(-) diff --git a/xarray/core/_reductions.py b/xarray/core/_reductions.py index a2108f54a64..8b462f893ba 100644 --- a/xarray/core/_reductions.py +++ b/xarray/core/_reductions.py @@ -735,6 +735,7 @@ def std( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = True, + ddof: int = 0, keep_attrs: bool = None, **kwargs, ) -> T_Dataset: @@ -751,6 +752,9 @@ def std( skips missing values for float dtypes; other dtypes either do not have a sentinel missing value (int) or skipna=True has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -803,6 +807,16 @@ def std( Data variables: da (labels) float64 nan 0.0 1.0 + Specify ``ddof=1`` for an unbiased estimate. + + >>> ds.groupby("labels").std(skipna=True, ddof=1) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 0.0 1.414 + See Also -------- numpy.std @@ -814,6 +828,7 @@ def std( duck_array_ops.std, dim=dim, skipna=skipna, + ddof=ddof, numeric_only=True, keep_attrs=keep_attrs, **kwargs, @@ -823,6 +838,7 @@ def var( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = True, + ddof: int = 0, keep_attrs: bool = None, **kwargs, ) -> T_Dataset: @@ -839,6 +855,9 @@ def var( skips missing values for float dtypes; other dtypes either do not have a sentinel missing value (int) or skipna=True has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -891,6 +910,16 @@ def var( Data variables: da (labels) float64 nan 0.0 1.0 + Specify ``ddof=1`` for an unbiased estimate. + + >>> ds.groupby("labels").var(skipna=True, ddof=1) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 0.0 2.0 + See Also -------- numpy.var @@ -902,6 +931,7 @@ def var( duck_array_ops.var, dim=dim, skipna=skipna, + ddof=ddof, numeric_only=True, keep_attrs=keep_attrs, **kwargs, @@ -1692,6 +1722,7 @@ def std( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = True, + ddof: int = 0, keep_attrs: bool = None, **kwargs, ) -> T_Dataset: @@ -1708,6 +1739,9 @@ def std( skips missing values for float dtypes; other dtypes either do not have a sentinel missing value (int) or skipna=True has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -1760,6 +1794,16 @@ def std( Data variables: da (time) float64 0.0 0.8165 nan + Specify ``ddof=1`` for an unbiased estimate. + + >>> ds.resample(time="3M").std(skipna=True, ddof=1) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 nan 1.0 nan + See Also -------- numpy.std @@ -1771,6 +1815,7 @@ def std( duck_array_ops.std, dim=dim, skipna=skipna, + ddof=ddof, numeric_only=True, keep_attrs=keep_attrs, **kwargs, @@ -1780,6 +1825,7 @@ def var( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = True, + ddof: int = 0, keep_attrs: bool = None, **kwargs, ) -> T_Dataset: @@ -1796,6 +1842,9 @@ def var( skips missing values for float dtypes; other dtypes either do not have a sentinel missing value (int) or skipna=True has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -1848,6 +1897,16 @@ def var( Data variables: da (time) float64 0.0 0.6667 nan + Specify ``ddof=1`` for an unbiased estimate. + + >>> ds.resample(time="3M").var(skipna=True, ddof=1) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 nan 1.0 nan + See Also -------- numpy.var @@ -1859,6 +1918,7 @@ def var( duck_array_ops.var, dim=dim, skipna=skipna, + ddof=ddof, numeric_only=True, keep_attrs=keep_attrs, **kwargs, @@ -2587,6 +2647,7 @@ def std( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = True, + ddof: int = 0, keep_attrs: bool = None, **kwargs, ) -> T_DataArray: @@ -2603,6 +2664,9 @@ def std( skips missing values for float dtypes; other dtypes either do not have a sentinel missing value (int) or skipna=True has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -2648,6 +2712,14 @@ def std( Coordinates: * labels (labels) object 'a' 'b' 'c' + Specify ``ddof=1`` for an unbiased estimate. + + >>> da.groupby("labels").std(skipna=True, ddof=1) + + array([ nan, 0. , 1.41421356]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + See Also -------- numpy.std @@ -2659,6 +2731,7 @@ def std( duck_array_ops.std, dim=dim, skipna=skipna, + ddof=ddof, keep_attrs=keep_attrs, **kwargs, ) @@ -2667,6 +2740,7 @@ def var( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = True, + ddof: int = 0, keep_attrs: bool = None, **kwargs, ) -> T_DataArray: @@ -2683,6 +2757,9 @@ def var( skips missing values for float dtypes; other dtypes either do not have a sentinel missing value (int) or skipna=True has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -2728,6 +2805,14 @@ def var( Coordinates: * labels (labels) object 'a' 'b' 'c' + Specify ``ddof=1`` for an unbiased estimate. + + >>> da.groupby("labels").var(skipna=True, ddof=1) + + array([nan, 0., 2.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + See Also -------- numpy.var @@ -2739,6 +2824,7 @@ def var( duck_array_ops.var, dim=dim, skipna=skipna, + ddof=ddof, keep_attrs=keep_attrs, **kwargs, ) @@ -3458,6 +3544,7 @@ def std( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = True, + ddof: int = 0, keep_attrs: bool = None, **kwargs, ) -> T_DataArray: @@ -3474,6 +3561,9 @@ def std( skips missing values for float dtypes; other dtypes either do not have a sentinel missing value (int) or skipna=True has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -3519,6 +3609,14 @@ def std( Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Specify ``ddof=1`` for an unbiased estimate. + + >>> da.resample(time="3M").std(skipna=True, ddof=1) + + array([nan, 1., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + See Also -------- numpy.std @@ -3530,6 +3628,7 @@ def std( duck_array_ops.std, dim=dim, skipna=skipna, + ddof=ddof, keep_attrs=keep_attrs, **kwargs, ) @@ -3538,6 +3637,7 @@ def var( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = True, + ddof: int = 0, keep_attrs: bool = None, **kwargs, ) -> T_DataArray: @@ -3554,6 +3654,9 @@ def var( skips missing values for float dtypes; other dtypes either do not have a sentinel missing value (int) or skipna=True has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -3599,6 +3702,14 @@ def var( Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Specify ``ddof=1`` for an unbiased estimate. + + >>> da.resample(time="3M").var(skipna=True, ddof=1) + + array([nan, 1., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + See Also -------- numpy.var @@ -3610,6 +3721,7 @@ def var( duck_array_ops.var, dim=dim, skipna=skipna, + ddof=ddof, keep_attrs=keep_attrs, **kwargs, ) diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index fbda0b6d6de..dda6415262c 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -97,6 +97,10 @@ def {method}( array's dtype. Changed in version 0.17.0: if specified on an integer array and skipna=True, the result will be a float array.""" +_DDOF_DOCSTRING = """ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements.""" + _KEEP_ATTRS_DOCSTRING = """keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -132,6 +136,15 @@ def {method}( >>> {calculation}(skipna=True, min_count=2)""", ) +ddof = extra_kwarg( + docs=_DDOF_DOCSTRING, + kwarg="ddof: int = 0,", + call="ddof=ddof,", + example="""\n + Specify ``ddof=1`` for an unbiased estimate. + + >>> {calculation}(skipna=True, ddof=1)""", +) class Method: @@ -275,8 +288,8 @@ def generate_code(self, method): Method("mean", extra_kwargs=(skip_na,), numeric_only=True), Method("prod", extra_kwargs=(skip_na, min_count), numeric_only=True), Method("sum", extra_kwargs=(skip_na, min_count), numeric_only=True), - Method("std", extra_kwargs=(skip_na,), numeric_only=True), - Method("var", extra_kwargs=(skip_na,), numeric_only=True), + Method("std", extra_kwargs=(skip_na, ddof), numeric_only=True), + Method("var", extra_kwargs=(skip_na, ddof), numeric_only=True), Method("median", extra_kwargs=(skip_na,), numeric_only=True), ) From 816e7941e47b14280103d5d10da94139f394c0cd Mon Sep 17 00:00:00 2001 From: dcherian Date: Sun, 7 Nov 2021 20:56:37 -0700 Subject: [PATCH 03/31] Generate DataArray, Dataset reductions too. --- doc/user-guide/computation.rst | 2 + xarray/core/_reductions.py | 1984 +++++++++++++++++++++++++-- xarray/core/arithmetic.py | 2 - xarray/core/dataarray.py | 5 +- xarray/core/dataset.py | 3 +- xarray/tests/test_duck_array_ops.py | 81 -- xarray/util/generate_reductions.py | 44 +- 7 files changed, 1928 insertions(+), 193 deletions(-) diff --git a/doc/user-guide/computation.rst b/doc/user-guide/computation.rst index fc3c457308f..a4ba606feeb 100644 --- a/doc/user-guide/computation.rst +++ b/doc/user-guide/computation.rst @@ -107,6 +107,8 @@ Xarray also provides the ``max_gap`` keyword argument to limit the interpolation data gaps of length ``max_gap`` or smaller. See :py:meth:`~xarray.DataArray.interpolate_na` for more. +.. _agg: + Aggregation =========== diff --git a/xarray/core/_reductions.py b/xarray/core/_reductions.py index 8b462f893ba..1314b6877db 100644 --- a/xarray/core/_reductions.py +++ b/xarray/core/_reductions.py @@ -39,6 +39,1798 @@ def reduce( ... +class DatasetReductions: + __slots__ = () + + def count( + self: DatasetReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_Dataset: + """ + Reduce this Dataset's data by applying ``count`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``count`` on this object's data. + + Returns + ------- + reduced : Dataset + New Dataset with ``count`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.count() + + Dimensions: () + Data variables: + da int64 5 + + See Also + -------- + numpy.count + Dataset.count + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.count, + dim=dim, + numeric_only=False, + keep_attrs=keep_attrs, + **kwargs, + ) + + def all( + self: DatasetReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_Dataset: + """ + Reduce this Dataset's data by applying ``all`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``all`` on this object's data. + + Returns + ------- + reduced : Dataset + New Dataset with ``all`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([True, True, True, True, True, False], dtype=bool), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.all() + + Dimensions: () + Data variables: + da bool False + + See Also + -------- + numpy.all + Dataset.all + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.array_all, + dim=dim, + numeric_only=False, + keep_attrs=keep_attrs, + **kwargs, + ) + + def any( + self: DatasetReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_Dataset: + """ + Reduce this Dataset's data by applying ``any`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``any`` on this object's data. + + Returns + ------- + reduced : Dataset + New Dataset with ``any`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([True, True, True, True, True, False], dtype=bool), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.any() + + Dimensions: () + Data variables: + da bool True + + See Also + -------- + numpy.any + Dataset.any + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.array_any, + dim=dim, + numeric_only=False, + keep_attrs=keep_attrs, + **kwargs, + ) + + def max( + self: DatasetReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_Dataset: + """ + Reduce this Dataset's data by applying ``max`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``max`` on this object's data. + + Returns + ------- + reduced : Dataset + New Dataset with ``max`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.max() + + Dimensions: () + Data variables: + da float64 3.0 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.max(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + + See Also + -------- + numpy.max + Dataset.max + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.max, + dim=dim, + skipna=skipna, + numeric_only=False, + keep_attrs=keep_attrs, + **kwargs, + ) + + def min( + self: DatasetReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_Dataset: + """ + Reduce this Dataset's data by applying ``min`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``min`` on this object's data. + + Returns + ------- + reduced : Dataset + New Dataset with ``min`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.min() + + Dimensions: () + Data variables: + da float64 1.0 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.min(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + + See Also + -------- + numpy.min + Dataset.min + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.min, + dim=dim, + skipna=skipna, + numeric_only=False, + keep_attrs=keep_attrs, + **kwargs, + ) + + def mean( + self: DatasetReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_Dataset: + """ + Reduce this Dataset's data by applying ``mean`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``mean`` on this object's data. + + Returns + ------- + reduced : Dataset + New Dataset with ``mean`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.mean() + + Dimensions: () + Data variables: + da float64 1.8 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.mean(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + + See Also + -------- + numpy.mean + Dataset.mean + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.mean, + dim=dim, + skipna=skipna, + numeric_only=True, + keep_attrs=keep_attrs, + **kwargs, + ) + + def prod( + self: DatasetReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + min_count: Optional[int] = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_Dataset: + """ + Reduce this Dataset's data by applying ``prod`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + min_count : int, default: None + The required number of valid values to perform the operation. If + fewer than min_count non-NA values are present the result will be + NA. Only used if skipna is set to True or defaults to True for the + array's dtype. Changed in version 0.17.0: if specified on an integer + array and skipna=True, the result will be a float array. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``prod`` on this object's data. + + Returns + ------- + reduced : Dataset + New Dataset with ``prod`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.prod() + + Dimensions: () + Data variables: + da float64 12.0 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.prod(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + + Specify ``min_count`` for finer control over when NaNs are ignored. + + >>> ds.prod(skipna=True, min_count=2) + + Dimensions: () + Data variables: + da float64 12.0 + + See Also + -------- + numpy.prod + Dataset.prod + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.prod, + dim=dim, + skipna=skipna, + min_count=min_count, + numeric_only=True, + keep_attrs=keep_attrs, + **kwargs, + ) + + def sum( + self: DatasetReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + min_count: Optional[int] = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_Dataset: + """ + Reduce this Dataset's data by applying ``sum`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + min_count : int, default: None + The required number of valid values to perform the operation. If + fewer than min_count non-NA values are present the result will be + NA. Only used if skipna is set to True or defaults to True for the + array's dtype. Changed in version 0.17.0: if specified on an integer + array and skipna=True, the result will be a float array. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``sum`` on this object's data. + + Returns + ------- + reduced : Dataset + New Dataset with ``sum`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.sum() + + Dimensions: () + Data variables: + da float64 9.0 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.sum(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + + Specify ``min_count`` for finer control over when NaNs are ignored. + + >>> ds.sum(skipna=True, min_count=2) + + Dimensions: () + Data variables: + da float64 9.0 + + See Also + -------- + numpy.sum + Dataset.sum + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.sum, + dim=dim, + skipna=skipna, + min_count=min_count, + numeric_only=True, + keep_attrs=keep_attrs, + **kwargs, + ) + + def std( + self: DatasetReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + ddof: int = 0, + keep_attrs: bool = None, + **kwargs, + ) -> T_Dataset: + """ + Reduce this Dataset's data by applying ``std`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``std`` on this object's data. + + Returns + ------- + reduced : Dataset + New Dataset with ``std`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.std() + + Dimensions: () + Data variables: + da float64 0.7483 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.std(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + + Specify ``ddof=1`` for an unbiased estimate. + + >>> ds.std(skipna=True, ddof=1) + + Dimensions: () + Data variables: + da float64 0.8367 + + See Also + -------- + numpy.std + Dataset.std + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.std, + dim=dim, + skipna=skipna, + ddof=ddof, + numeric_only=True, + keep_attrs=keep_attrs, + **kwargs, + ) + + def var( + self: DatasetReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + ddof: int = 0, + keep_attrs: bool = None, + **kwargs, + ) -> T_Dataset: + """ + Reduce this Dataset's data by applying ``var`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``var`` on this object's data. + + Returns + ------- + reduced : Dataset + New Dataset with ``var`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.var() + + Dimensions: () + Data variables: + da float64 0.56 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.var(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + + Specify ``ddof=1`` for an unbiased estimate. + + >>> ds.var(skipna=True, ddof=1) + + Dimensions: () + Data variables: + da float64 0.7 + + See Also + -------- + numpy.var + Dataset.var + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.var, + dim=dim, + skipna=skipna, + ddof=ddof, + numeric_only=True, + keep_attrs=keep_attrs, + **kwargs, + ) + + def median( + self: DatasetReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_Dataset: + """ + Reduce this Dataset's data by applying ``median`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``median`` on this object's data. + + Returns + ------- + reduced : Dataset + New Dataset with ``median`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.median() + + Dimensions: () + Data variables: + da float64 2.0 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.median(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + + See Also + -------- + numpy.median + Dataset.median + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.median, + dim=dim, + skipna=skipna, + numeric_only=True, + keep_attrs=keep_attrs, + **kwargs, + ) + + +class DataArrayReductions: + __slots__ = () + + def count( + self: DataArrayReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_DataArray: + """ + Reduce this DataArray's data by applying ``count`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``count`` on this object's data. + + Returns + ------- + reduced : DataArray + New DataArray with ``count`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.count() + + array(5) + + See Also + -------- + numpy.count + DataArray.count + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.count, + dim=dim, + keep_attrs=keep_attrs, + **kwargs, + ) + + def all( + self: DataArrayReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_DataArray: + """ + Reduce this DataArray's data by applying ``all`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``all`` on this object's data. + + Returns + ------- + reduced : DataArray + New DataArray with ``all`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([True, True, True, True, True, False], dtype=bool), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.all() + + array(False) + + See Also + -------- + numpy.all + DataArray.all + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.array_all, + dim=dim, + keep_attrs=keep_attrs, + **kwargs, + ) + + def any( + self: DataArrayReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_DataArray: + """ + Reduce this DataArray's data by applying ``any`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``any`` on this object's data. + + Returns + ------- + reduced : DataArray + New DataArray with ``any`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([True, True, True, True, True, False], dtype=bool), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.any() + + array(True) + + See Also + -------- + numpy.any + DataArray.any + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.array_any, + dim=dim, + keep_attrs=keep_attrs, + **kwargs, + ) + + def max( + self: DataArrayReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_DataArray: + """ + Reduce this DataArray's data by applying ``max`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``max`` on this object's data. + + Returns + ------- + reduced : DataArray + New DataArray with ``max`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.max() + + array(3.) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.max(skipna=False) + + array(nan) + + See Also + -------- + numpy.max + DataArray.max + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.max, + dim=dim, + skipna=skipna, + keep_attrs=keep_attrs, + **kwargs, + ) + + def min( + self: DataArrayReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_DataArray: + """ + Reduce this DataArray's data by applying ``min`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``min`` on this object's data. + + Returns + ------- + reduced : DataArray + New DataArray with ``min`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.min() + + array(1.) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.min(skipna=False) + + array(nan) + + See Also + -------- + numpy.min + DataArray.min + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.min, + dim=dim, + skipna=skipna, + keep_attrs=keep_attrs, + **kwargs, + ) + + def mean( + self: DataArrayReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_DataArray: + """ + Reduce this DataArray's data by applying ``mean`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``mean`` on this object's data. + + Returns + ------- + reduced : DataArray + New DataArray with ``mean`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.mean() + + array(1.8) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.mean(skipna=False) + + array(nan) + + See Also + -------- + numpy.mean + DataArray.mean + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.mean, + dim=dim, + skipna=skipna, + keep_attrs=keep_attrs, + **kwargs, + ) + + def prod( + self: DataArrayReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + min_count: Optional[int] = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_DataArray: + """ + Reduce this DataArray's data by applying ``prod`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + min_count : int, default: None + The required number of valid values to perform the operation. If + fewer than min_count non-NA values are present the result will be + NA. Only used if skipna is set to True or defaults to True for the + array's dtype. Changed in version 0.17.0: if specified on an integer + array and skipna=True, the result will be a float array. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``prod`` on this object's data. + + Returns + ------- + reduced : DataArray + New DataArray with ``prod`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.prod() + + array(12.) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.prod(skipna=False) + + array(nan) + + Specify ``min_count`` for finer control over when NaNs are ignored. + + >>> da.prod(skipna=True, min_count=2) + + array(12.) + + See Also + -------- + numpy.prod + DataArray.prod + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.prod, + dim=dim, + skipna=skipna, + min_count=min_count, + keep_attrs=keep_attrs, + **kwargs, + ) + + def sum( + self: DataArrayReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + min_count: Optional[int] = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_DataArray: + """ + Reduce this DataArray's data by applying ``sum`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + min_count : int, default: None + The required number of valid values to perform the operation. If + fewer than min_count non-NA values are present the result will be + NA. Only used if skipna is set to True or defaults to True for the + array's dtype. Changed in version 0.17.0: if specified on an integer + array and skipna=True, the result will be a float array. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``sum`` on this object's data. + + Returns + ------- + reduced : DataArray + New DataArray with ``sum`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.sum() + + array(9.) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.sum(skipna=False) + + array(nan) + + Specify ``min_count`` for finer control over when NaNs are ignored. + + >>> da.sum(skipna=True, min_count=2) + + array(9.) + + See Also + -------- + numpy.sum + DataArray.sum + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.sum, + dim=dim, + skipna=skipna, + min_count=min_count, + keep_attrs=keep_attrs, + **kwargs, + ) + + def std( + self: DataArrayReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + ddof: int = 0, + keep_attrs: bool = None, + **kwargs, + ) -> T_DataArray: + """ + Reduce this DataArray's data by applying ``std`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``std`` on this object's data. + + Returns + ------- + reduced : DataArray + New DataArray with ``std`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.std() + + array(0.74833148) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.std(skipna=False) + + array(nan) + + Specify ``ddof=1`` for an unbiased estimate. + + >>> da.std(skipna=True, ddof=1) + + array(0.83666003) + + See Also + -------- + numpy.std + DataArray.std + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.std, + dim=dim, + skipna=skipna, + ddof=ddof, + keep_attrs=keep_attrs, + **kwargs, + ) + + def var( + self: DataArrayReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + ddof: int = 0, + keep_attrs: bool = None, + **kwargs, + ) -> T_DataArray: + """ + Reduce this DataArray's data by applying ``var`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``var`` on this object's data. + + Returns + ------- + reduced : DataArray + New DataArray with ``var`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.var() + + array(0.56) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.var(skipna=False) + + array(nan) + + Specify ``ddof=1`` for an unbiased estimate. + + >>> da.var(skipna=True, ddof=1) + + array(0.7) + + See Also + -------- + numpy.var + DataArray.var + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.var, + dim=dim, + skipna=skipna, + ddof=ddof, + keep_attrs=keep_attrs, + **kwargs, + ) + + def median( + self: DataArrayReduce, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> T_DataArray: + """ + Reduce this DataArray's data by applying ``median`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, optional + Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``median`` on this object's data. + + Returns + ------- + reduced : DataArray + New DataArray with ``median`` applied to its data and the + indicated dimension(s) removed + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.median() + + array(2.) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.median(skipna=False) + + array(nan) + + See Also + -------- + numpy.median + DataArray.median + :ref:`agg` + User guide on reduction or aggregation operations. + """ + return self.reduce( + duck_array_ops.median, + dim=dim, + skipna=skipna, + keep_attrs=keep_attrs, + **kwargs, + ) + + class DatasetGroupByReductions: __slots__ = () @@ -258,7 +2050,7 @@ def any( def max( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_Dataset: @@ -270,10 +2062,10 @@ def max( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -346,7 +2138,7 @@ def max( def min( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_Dataset: @@ -358,10 +2150,10 @@ def min( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -434,7 +2226,7 @@ def min( def mean( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_Dataset: @@ -446,10 +2238,10 @@ def mean( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -522,7 +2314,7 @@ def mean( def prod( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, @@ -535,10 +2327,10 @@ def prod( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -628,7 +2420,7 @@ def prod( def sum( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, @@ -641,10 +2433,10 @@ def sum( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -734,7 +2526,7 @@ def sum( def std( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, @@ -747,10 +2539,10 @@ def std( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). ddof : int, default: 0 “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, @@ -837,7 +2629,7 @@ def std( def var( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, @@ -850,10 +2642,10 @@ def var( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). ddof : int, default: 0 “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, @@ -940,7 +2732,7 @@ def var( def median( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_Dataset: @@ -952,10 +2744,10 @@ def median( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -1245,7 +3037,7 @@ def any( def max( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_Dataset: @@ -1257,10 +3049,10 @@ def max( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -1333,7 +3125,7 @@ def max( def min( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_Dataset: @@ -1345,10 +3137,10 @@ def min( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -1421,7 +3213,7 @@ def min( def mean( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_Dataset: @@ -1433,10 +3225,10 @@ def mean( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -1509,7 +3301,7 @@ def mean( def prod( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, @@ -1522,10 +3314,10 @@ def prod( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -1615,7 +3407,7 @@ def prod( def sum( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, @@ -1628,10 +3420,10 @@ def sum( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -1721,7 +3513,7 @@ def sum( def std( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, @@ -1734,10 +3526,10 @@ def std( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). ddof : int, default: 0 “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, @@ -1824,7 +3616,7 @@ def std( def var( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, @@ -1837,10 +3629,10 @@ def var( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). ddof : int, default: 0 “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, @@ -1927,7 +3719,7 @@ def var( def median( self: DatasetReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_Dataset: @@ -1939,10 +3731,10 @@ def median( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -2214,7 +4006,7 @@ def any( def max( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_DataArray: @@ -2226,10 +4018,10 @@ def max( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -2294,7 +4086,7 @@ def max( def min( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_DataArray: @@ -2306,10 +4098,10 @@ def min( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -2374,7 +4166,7 @@ def min( def mean( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_DataArray: @@ -2386,10 +4178,10 @@ def mean( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -2454,7 +4246,7 @@ def mean( def prod( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, @@ -2467,10 +4259,10 @@ def prod( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -2550,7 +4342,7 @@ def prod( def sum( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, @@ -2563,10 +4355,10 @@ def sum( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -2646,7 +4438,7 @@ def sum( def std( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, @@ -2659,10 +4451,10 @@ def std( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). ddof : int, default: 0 “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, @@ -2739,7 +4531,7 @@ def std( def var( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, @@ -2752,10 +4544,10 @@ def var( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). ddof : int, default: 0 “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, @@ -2832,7 +4624,7 @@ def var( def median( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_DataArray: @@ -2844,10 +4636,10 @@ def median( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -3111,7 +4903,7 @@ def any( def max( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_DataArray: @@ -3123,10 +4915,10 @@ def max( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -3191,7 +4983,7 @@ def max( def min( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_DataArray: @@ -3203,10 +4995,10 @@ def min( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -3271,7 +5063,7 @@ def min( def mean( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_DataArray: @@ -3283,10 +5075,10 @@ def mean( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -3351,7 +5143,7 @@ def mean( def prod( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, @@ -3364,10 +5156,10 @@ def prod( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -3447,7 +5239,7 @@ def prod( def sum( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, @@ -3460,10 +5252,10 @@ def sum( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -3543,7 +5335,7 @@ def sum( def std( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, @@ -3556,10 +5348,10 @@ def std( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). ddof : int, default: 0 “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, @@ -3636,7 +5428,7 @@ def std( def var( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, @@ -3649,10 +5441,10 @@ def var( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). ddof : int, default: 0 “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, @@ -3729,7 +5521,7 @@ def var( def median( self: DataArrayReduce, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + skipna: bool = None, keep_attrs: bool = None, **kwargs, ) -> T_DataArray: @@ -3741,10 +5533,10 @@ def median( dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. - skipna : bool, optional + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original diff --git a/xarray/core/arithmetic.py b/xarray/core/arithmetic.py index 814e9a59877..bf8d6ccaeb6 100644 --- a/xarray/core/arithmetic.py +++ b/xarray/core/arithmetic.py @@ -105,7 +105,6 @@ class VariableArithmetic( class DatasetArithmetic( ImplementsDatasetReduce, - IncludeReduceMethods, IncludeCumMethods, SupportsArithmetic, DatasetOpsMixin, @@ -116,7 +115,6 @@ class DatasetArithmetic( class DataArrayArithmetic( ImplementsArrayReduce, - IncludeReduceMethods, IncludeCumMethods, IncludeNumpySameMethods, SupportsArithmetic, diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 89f916db7f4..a991eb05e1d 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -35,6 +35,7 @@ utils, weighted, ) +from ._reductions import DataArrayReductions from .accessor_dt import CombinedDatetimelikeAccessor from .accessor_str import StringAccessor from .alignment import ( @@ -215,7 +216,9 @@ def __setitem__(self, key, value) -> None: _THIS_ARRAY = ReprObject("") -class DataArray(AbstractArray, DataWithCoords, DataArrayArithmetic): +class DataArray( + AbstractArray, DataWithCoords, DataArrayArithmetic, DataArrayReductions +): """N-dimensional array with labeled coordinates and dimensions. DataArray provides a wrapper around numpy ndarrays that uses diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index e882495dce5..e3339f2562f 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -50,6 +50,7 @@ utils, weighted, ) +from ._reductions import DatasetReductions from .alignment import _broadcast_helper, _get_broadcast_dims_map_common_coords, align from .arithmetic import DatasetArithmetic from .common import DataWithCoords, _contains_datetime_like_objects, get_chunksizes @@ -574,7 +575,7 @@ def __setitem__(self, key, value) -> None: self.dataset[pos_indexers] = value -class Dataset(DataWithCoords, DatasetArithmetic, Mapping): +class Dataset(DataWithCoords, DatasetReductions, DatasetArithmetic, Mapping): """A multi-dimensional, in memory, array database. A dataset resembles an in-memory representation of a NetCDF file, diff --git a/xarray/tests/test_duck_array_ops.py b/xarray/tests/test_duck_array_ops.py index c032a781e47..392597f1bda 100644 --- a/xarray/tests/test_duck_array_ops.py +++ b/xarray/tests/test_duck_array_ops.py @@ -676,87 +676,6 @@ def test_multiple_dims(dtype, dask, skipna, func): assert_allclose(actual, expected) -def test_docs(): - # with min_count - actual = DataArray.sum.__doc__ - expected = dedent( - """\ - Reduce this DataArray's data by applying `sum` along some dimension(s). - - Parameters - ---------- - dim : str or sequence of str, optional - Dimension(s) over which to apply `sum`. - axis : int or sequence of int, optional - Axis(es) over which to apply `sum`. Only one of the 'dim' - and 'axis' arguments can be supplied. If neither are supplied, then - `sum` is calculated over axes. - skipna : bool, optional - If True, skip missing values (as marked by NaN). By default, only - skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been - implemented (object, datetime64 or timedelta64). - min_count : int, default: None - The required number of valid values to perform the operation. If - fewer than min_count non-NA values are present the result will be - NA. Only used if skipna is set to True or defaults to True for the - array's dtype. New in version 0.10.8: Added with the default being - None. Changed in version 0.17.0: if specified on an integer array - and skipna=True, the result will be a float array. - keep_attrs : bool, optional - If True, the attributes (`attrs`) will be copied from the original - object to the new one. If False (default), the new object will be - returned without attributes. - **kwargs : dict - Additional keyword arguments passed on to the appropriate array - function for calculating `sum` on this object's data. - - Returns - ------- - reduced : DataArray - New DataArray object with `sum` applied to its data and the - indicated dimension(s) removed. - """ - ) - assert actual == expected - - # without min_count - actual = DataArray.std.__doc__ - expected = dedent( - """\ - Reduce this DataArray's data by applying `std` along some dimension(s). - - Parameters - ---------- - dim : str or sequence of str, optional - Dimension(s) over which to apply `std`. - axis : int or sequence of int, optional - Axis(es) over which to apply `std`. Only one of the 'dim' - and 'axis' arguments can be supplied. If neither are supplied, then - `std` is calculated over axes. - skipna : bool, optional - If True, skip missing values (as marked by NaN). By default, only - skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been - implemented (object, datetime64 or timedelta64). - keep_attrs : bool, optional - If True, the attributes (`attrs`) will be copied from the original - object to the new one. If False (default), the new object will be - returned without attributes. - **kwargs : dict - Additional keyword arguments passed on to the appropriate array - function for calculating `std` on this object's data. - - Returns - ------- - reduced : DataArray - New DataArray object with `std` applied to its data and the - indicated dimension(s) removed. - """ - ) - assert actual == expected - - def test_datetime_to_numeric_datetime64(): times = pd.date_range("2000", periods=5, freq="7D").values result = duck_array_ops.datetime_to_numeric(times, datetime_unit="h") diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index dda6415262c..284cca38603 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -12,9 +12,9 @@ while replacing the doctests. """ - import collections import textwrap +from dataclasses import dataclass MODULE_PREAMBLE = '''\ """Mixin classes with reduction operations.""" @@ -84,10 +84,10 @@ def {method}( Name of dimension[s] along which to apply ``{method}``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions.""" -_SKIPNA_DOCSTRING = """skipna : bool, optional +_SKIPNA_DOCSTRING = """skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64).""" _MINCOUNT_DOCSTRING = """min_count : int, default: None @@ -120,7 +120,7 @@ def {method}( extra_kwarg = collections.namedtuple("extra_kwarg", "docs kwarg call example") skip_na = extra_kwarg( docs=_SKIPNA_DOCSTRING, - kwarg="skipna: bool = True,", + kwarg="skipna: bool = None,", call="skipna=skipna,", example="""\n Use ``skipna`` to control whether NaNs are ignored. @@ -170,14 +170,6 @@ def __init__( ... np.array([1, 2, 3, 1, 2, np.nan]),""" -class DataStructure: - def __init__(self, name, docstring_create, example_var_name, numeric_only=False): - self.name = name - self.docstring_create = docstring_create - self.example_var_name = example_var_name - self.numeric_only = numeric_only - - class ClassReductionGenerator: def __init__( self, @@ -293,6 +285,15 @@ def generate_code(self, method): Method("median", extra_kwargs=(skip_na,), numeric_only=True), ) + +@dataclass +class DataStructure: + name: str + docstring_create: str + example_var_name: str + numeric_only: bool = False + + DatasetObject = DataStructure( name="Dataset", docstring_create=""" @@ -309,6 +310,23 @@ def generate_code(self, method): numeric_only=False, ) +DatasetGenerator = ClassReductionGenerator( + cls="", + datastructure=DatasetObject, + methods=METHODS, + docref="agg", + docref_description="reduction or aggregation operations", + example_call_preamble="", +) +DataArrayGenerator = ClassReductionGenerator( + cls="", + datastructure=DataArrayObject, + methods=METHODS, + docref="agg", + docref_description="reduction or aggregation operations", + example_call_preamble="", +) + DataArrayGroupByGenerator = ClassReductionGenerator( cls="GroupBy", datastructure=DataArrayObject, @@ -348,6 +366,8 @@ def generate_code(self, method): print(OBJ_PREAMBLE.format(obj="Dataset")) print(OBJ_PREAMBLE.format(obj="DataArray")) for gen in [ + DatasetGenerator, + DataArrayGenerator, DatasetGroupByGenerator, DatasetResampleGenerator, DataArrayGroupByGenerator, From a04ed824a55b757937a4db4aa65729dccf62c1a7 Mon Sep 17 00:00:00 2001 From: dcherian Date: Mon, 8 Nov 2021 12:35:48 -0700 Subject: [PATCH 04/31] Small changes --- xarray/util/generate_reductions.py | 36 ++++++++++++++++-------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index 284cca38603..69d1c7db529 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -75,6 +75,7 @@ def {method}( See Also -------- numpy.{method} + dask.array.{method} {obj}.{method} :ref:`{docref}` User guide on {docref_description}. @@ -108,7 +109,8 @@ def {method}( _KWARGS_DOCSTRING = """**kwargs : dict Additional keyword arguments passed on to the appropriate array - function for calculating ``{method}`` on this object's data.""" + function for calculating ``{method}`` on this object's data. + These could include dask-specific kwargs like ``split_every``.""" NAN_CUM_METHODS = ["cumsum", "cumprod"] @@ -118,7 +120,7 @@ def {method}( ] extra_kwarg = collections.namedtuple("extra_kwarg", "docs kwarg call example") -skip_na = extra_kwarg( +skipna = extra_kwarg( docs=_SKIPNA_DOCSTRING, kwarg="skipna: bool = None,", call="skipna=skipna,", @@ -271,18 +273,18 @@ def generate_code(self, method): )""" -METHODS = ( +REDUCTION_METHODS = ( Method("count"), Method("all", bool_reduce=True), Method("any", bool_reduce=True), - Method("max", extra_kwargs=(skip_na,)), - Method("min", extra_kwargs=(skip_na,)), - Method("mean", extra_kwargs=(skip_na,), numeric_only=True), - Method("prod", extra_kwargs=(skip_na, min_count), numeric_only=True), - Method("sum", extra_kwargs=(skip_na, min_count), numeric_only=True), - Method("std", extra_kwargs=(skip_na, ddof), numeric_only=True), - Method("var", extra_kwargs=(skip_na, ddof), numeric_only=True), - Method("median", extra_kwargs=(skip_na,), numeric_only=True), + Method("max", extra_kwargs=(skipna,)), + Method("min", extra_kwargs=(skipna,)), + Method("mean", extra_kwargs=(skipna,), numeric_only=True), + Method("prod", extra_kwargs=(skipna, min_count), numeric_only=True), + Method("sum", extra_kwargs=(skipna, min_count), numeric_only=True), + Method("std", extra_kwargs=(skipna, ddof), numeric_only=True), + Method("var", extra_kwargs=(skipna, ddof), numeric_only=True), + Method("median", extra_kwargs=(skipna,), numeric_only=True), ) @@ -313,7 +315,7 @@ class DataStructure: DatasetGenerator = ClassReductionGenerator( cls="", datastructure=DatasetObject, - methods=METHODS, + methods=REDUCTION_METHODS, docref="agg", docref_description="reduction or aggregation operations", example_call_preamble="", @@ -321,7 +323,7 @@ class DataStructure: DataArrayGenerator = ClassReductionGenerator( cls="", datastructure=DataArrayObject, - methods=METHODS, + methods=REDUCTION_METHODS, docref="agg", docref_description="reduction or aggregation operations", example_call_preamble="", @@ -330,7 +332,7 @@ class DataStructure: DataArrayGroupByGenerator = ClassReductionGenerator( cls="GroupBy", datastructure=DataArrayObject, - methods=METHODS, + methods=REDUCTION_METHODS, docref="groupby", docref_description="groupby operations", example_call_preamble='.groupby("labels")', @@ -338,7 +340,7 @@ class DataStructure: DataArrayResampleGenerator = ClassReductionGenerator( cls="Resample", datastructure=DataArrayObject, - methods=METHODS, + methods=REDUCTION_METHODS, docref="resampling", docref_description="resampling operations", example_call_preamble='.resample(time="3M")', @@ -346,7 +348,7 @@ class DataStructure: DatasetGroupByGenerator = ClassReductionGenerator( cls="GroupBy", datastructure=DatasetObject, - methods=METHODS, + methods=REDUCTION_METHODS, docref="groupby", docref_description="groupby operations", example_call_preamble='.groupby("labels")', @@ -354,7 +356,7 @@ class DataStructure: DatasetResampleGenerator = ClassReductionGenerator( cls="Resample", datastructure=DatasetObject, - methods=METHODS, + methods=REDUCTION_METHODS, docref="resampling", docref_description="resampling operations", example_call_preamble='.resample(time="3M")', From 7f39cc0d8c664e3fcf354536ed3a95882064b4b6 Mon Sep 17 00:00:00 2001 From: dcherian Date: Mon, 8 Nov 2021 12:39:00 -0700 Subject: [PATCH 05/31] Minor docstring improvements. --- xarray/core/_reductions.py | 176 +++++++++++++++++++++++++---- xarray/util/generate_reductions.py | 10 +- 2 files changed, 163 insertions(+), 23 deletions(-) diff --git a/xarray/core/_reductions.py b/xarray/core/_reductions.py index 1314b6877db..1d6d4f88970 100644 --- a/xarray/core/_reductions.py +++ b/xarray/core/_reductions.py @@ -63,6 +63,7 @@ def count( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``count`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -99,7 +100,8 @@ def count( See Also -------- numpy.count - Dataset.count + dask.array.count + DataArray.count :ref:`agg` User guide on reduction or aggregation operations. """ @@ -132,6 +134,7 @@ def all( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``all`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -168,7 +171,8 @@ def all( See Also -------- numpy.all - Dataset.all + dask.array.all + DataArray.all :ref:`agg` User guide on reduction or aggregation operations. """ @@ -201,6 +205,7 @@ def any( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``any`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -237,7 +242,8 @@ def any( See Also -------- numpy.any - Dataset.any + dask.array.any + DataArray.any :ref:`agg` User guide on reduction or aggregation operations. """ @@ -276,6 +282,7 @@ def max( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``max`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -320,7 +327,8 @@ def max( See Also -------- numpy.max - Dataset.max + dask.array.max + DataArray.max :ref:`agg` User guide on reduction or aggregation operations. """ @@ -360,6 +368,7 @@ def min( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``min`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -404,7 +413,8 @@ def min( See Also -------- numpy.min - Dataset.min + dask.array.min + DataArray.min :ref:`agg` User guide on reduction or aggregation operations. """ @@ -444,6 +454,7 @@ def mean( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``mean`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -488,7 +499,8 @@ def mean( See Also -------- numpy.mean - Dataset.mean + dask.array.mean + DataArray.mean :ref:`agg` User guide on reduction or aggregation operations. """ @@ -535,6 +547,7 @@ def prod( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``prod`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -587,7 +600,8 @@ def prod( See Also -------- numpy.prod - Dataset.prod + dask.array.prod + DataArray.prod :ref:`agg` User guide on reduction or aggregation operations. """ @@ -635,6 +649,7 @@ def sum( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``sum`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -687,7 +702,8 @@ def sum( See Also -------- numpy.sum - Dataset.sum + dask.array.sum + DataArray.sum :ref:`agg` User guide on reduction or aggregation operations. """ @@ -732,6 +748,7 @@ def std( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``std`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -784,7 +801,8 @@ def std( See Also -------- numpy.std - Dataset.std + dask.array.std + DataArray.std :ref:`agg` User guide on reduction or aggregation operations. """ @@ -829,6 +847,7 @@ def var( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``var`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -881,7 +900,8 @@ def var( See Also -------- numpy.var - Dataset.var + dask.array.var + DataArray.var :ref:`agg` User guide on reduction or aggregation operations. """ @@ -922,6 +942,7 @@ def median( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``median`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -966,7 +987,8 @@ def median( See Also -------- numpy.median - Dataset.median + dask.array.median + DataArray.median :ref:`agg` User guide on reduction or aggregation operations. """ @@ -1004,6 +1026,7 @@ def count( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``count`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1035,7 +1058,8 @@ def count( See Also -------- numpy.count - DataArray.count + dask.array.count + Dataset.count :ref:`agg` User guide on reduction or aggregation operations. """ @@ -1067,6 +1091,7 @@ def all( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``all`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1098,7 +1123,8 @@ def all( See Also -------- numpy.all - DataArray.all + dask.array.all + Dataset.all :ref:`agg` User guide on reduction or aggregation operations. """ @@ -1130,6 +1156,7 @@ def any( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``any`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1161,7 +1188,8 @@ def any( See Also -------- numpy.any - DataArray.any + dask.array.any + Dataset.any :ref:`agg` User guide on reduction or aggregation operations. """ @@ -1199,6 +1227,7 @@ def max( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``max`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1236,7 +1265,8 @@ def max( See Also -------- numpy.max - DataArray.max + dask.array.max + Dataset.max :ref:`agg` User guide on reduction or aggregation operations. """ @@ -1275,6 +1305,7 @@ def min( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``min`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1312,7 +1343,8 @@ def min( See Also -------- numpy.min - DataArray.min + dask.array.min + Dataset.min :ref:`agg` User guide on reduction or aggregation operations. """ @@ -1351,6 +1383,7 @@ def mean( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``mean`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1388,7 +1421,8 @@ def mean( See Also -------- numpy.mean - DataArray.mean + dask.array.mean + Dataset.mean :ref:`agg` User guide on reduction or aggregation operations. """ @@ -1434,6 +1468,7 @@ def prod( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``prod`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1477,7 +1512,8 @@ def prod( See Also -------- numpy.prod - DataArray.prod + dask.array.prod + Dataset.prod :ref:`agg` User guide on reduction or aggregation operations. """ @@ -1524,6 +1560,7 @@ def sum( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``sum`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1567,7 +1604,8 @@ def sum( See Also -------- numpy.sum - DataArray.sum + dask.array.sum + Dataset.sum :ref:`agg` User guide on reduction or aggregation operations. """ @@ -1611,6 +1649,7 @@ def std( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``std`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1654,7 +1693,8 @@ def std( See Also -------- numpy.std - DataArray.std + dask.array.std + Dataset.std :ref:`agg` User guide on reduction or aggregation operations. """ @@ -1698,6 +1738,7 @@ def var( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``var`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1741,7 +1782,8 @@ def var( See Also -------- numpy.var - DataArray.var + dask.array.var + Dataset.var :ref:`agg` User guide on reduction or aggregation operations. """ @@ -1781,6 +1823,7 @@ def median( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``median`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1818,7 +1861,8 @@ def median( See Also -------- numpy.median - DataArray.median + dask.array.median + Dataset.median :ref:`agg` User guide on reduction or aggregation operations. """ @@ -1855,6 +1899,7 @@ def count( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``count`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1893,6 +1938,7 @@ def count( See Also -------- numpy.count + dask.array.count Dataset.count :ref:`groupby` User guide on groupby operations. @@ -1926,6 +1972,7 @@ def all( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``all`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1964,6 +2011,7 @@ def all( See Also -------- numpy.all + dask.array.all Dataset.all :ref:`groupby` User guide on groupby operations. @@ -1997,6 +2045,7 @@ def any( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``any`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2035,6 +2084,7 @@ def any( See Also -------- numpy.any + dask.array.any Dataset.any :ref:`groupby` User guide on groupby operations. @@ -2074,6 +2124,7 @@ def max( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``max`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2122,6 +2173,7 @@ def max( See Also -------- numpy.max + dask.array.max Dataset.max :ref:`groupby` User guide on groupby operations. @@ -2162,6 +2214,7 @@ def min( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``min`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2210,6 +2263,7 @@ def min( See Also -------- numpy.min + dask.array.min Dataset.min :ref:`groupby` User guide on groupby operations. @@ -2250,6 +2304,7 @@ def mean( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``mean`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2298,6 +2353,7 @@ def mean( See Also -------- numpy.mean + dask.array.mean Dataset.mean :ref:`groupby` User guide on groupby operations. @@ -2345,6 +2401,7 @@ def prod( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``prod`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2403,6 +2460,7 @@ def prod( See Also -------- numpy.prod + dask.array.prod Dataset.prod :ref:`groupby` User guide on groupby operations. @@ -2451,6 +2509,7 @@ def sum( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``sum`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2509,6 +2568,7 @@ def sum( See Also -------- numpy.sum + dask.array.sum Dataset.sum :ref:`groupby` User guide on groupby operations. @@ -2554,6 +2614,7 @@ def std( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``std`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2612,6 +2673,7 @@ def std( See Also -------- numpy.std + dask.array.std Dataset.std :ref:`groupby` User guide on groupby operations. @@ -2657,6 +2719,7 @@ def var( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``var`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2715,6 +2778,7 @@ def var( See Also -------- numpy.var + dask.array.var Dataset.var :ref:`groupby` User guide on groupby operations. @@ -2756,6 +2820,7 @@ def median( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``median`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2804,6 +2869,7 @@ def median( See Also -------- numpy.median + dask.array.median Dataset.median :ref:`groupby` User guide on groupby operations. @@ -2842,6 +2908,7 @@ def count( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``count`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2880,6 +2947,7 @@ def count( See Also -------- numpy.count + dask.array.count Dataset.count :ref:`resampling` User guide on resampling operations. @@ -2913,6 +2981,7 @@ def all( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``all`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2951,6 +3020,7 @@ def all( See Also -------- numpy.all + dask.array.all Dataset.all :ref:`resampling` User guide on resampling operations. @@ -2984,6 +3054,7 @@ def any( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``any`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3022,6 +3093,7 @@ def any( See Also -------- numpy.any + dask.array.any Dataset.any :ref:`resampling` User guide on resampling operations. @@ -3061,6 +3133,7 @@ def max( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``max`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3109,6 +3182,7 @@ def max( See Also -------- numpy.max + dask.array.max Dataset.max :ref:`resampling` User guide on resampling operations. @@ -3149,6 +3223,7 @@ def min( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``min`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3197,6 +3272,7 @@ def min( See Also -------- numpy.min + dask.array.min Dataset.min :ref:`resampling` User guide on resampling operations. @@ -3237,6 +3313,7 @@ def mean( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``mean`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3285,6 +3362,7 @@ def mean( See Also -------- numpy.mean + dask.array.mean Dataset.mean :ref:`resampling` User guide on resampling operations. @@ -3332,6 +3410,7 @@ def prod( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``prod`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3390,6 +3469,7 @@ def prod( See Also -------- numpy.prod + dask.array.prod Dataset.prod :ref:`resampling` User guide on resampling operations. @@ -3438,6 +3518,7 @@ def sum( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``sum`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3496,6 +3577,7 @@ def sum( See Also -------- numpy.sum + dask.array.sum Dataset.sum :ref:`resampling` User guide on resampling operations. @@ -3541,6 +3623,7 @@ def std( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``std`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3599,6 +3682,7 @@ def std( See Also -------- numpy.std + dask.array.std Dataset.std :ref:`resampling` User guide on resampling operations. @@ -3644,6 +3728,7 @@ def var( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``var`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3702,6 +3787,7 @@ def var( See Also -------- numpy.var + dask.array.var Dataset.var :ref:`resampling` User guide on resampling operations. @@ -3743,6 +3829,7 @@ def median( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``median`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3791,6 +3878,7 @@ def median( See Also -------- numpy.median + dask.array.median Dataset.median :ref:`resampling` User guide on resampling operations. @@ -3829,6 +3917,7 @@ def count( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``count`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3862,6 +3951,7 @@ def count( See Also -------- numpy.count + dask.array.count DataArray.count :ref:`groupby` User guide on groupby operations. @@ -3894,6 +3984,7 @@ def all( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``all`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3927,6 +4018,7 @@ def all( See Also -------- numpy.all + dask.array.all DataArray.all :ref:`groupby` User guide on groupby operations. @@ -3959,6 +4051,7 @@ def any( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``any`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3992,6 +4085,7 @@ def any( See Also -------- numpy.any + dask.array.any DataArray.any :ref:`groupby` User guide on groupby operations. @@ -4030,6 +4124,7 @@ def max( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``max`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -4071,6 +4166,7 @@ def max( See Also -------- numpy.max + dask.array.max DataArray.max :ref:`groupby` User guide on groupby operations. @@ -4110,6 +4206,7 @@ def min( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``min`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -4151,6 +4248,7 @@ def min( See Also -------- numpy.min + dask.array.min DataArray.min :ref:`groupby` User guide on groupby operations. @@ -4190,6 +4288,7 @@ def mean( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``mean`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -4231,6 +4330,7 @@ def mean( See Also -------- numpy.mean + dask.array.mean DataArray.mean :ref:`groupby` User guide on groupby operations. @@ -4277,6 +4377,7 @@ def prod( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``prod`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -4326,6 +4427,7 @@ def prod( See Also -------- numpy.prod + dask.array.prod DataArray.prod :ref:`groupby` User guide on groupby operations. @@ -4373,6 +4475,7 @@ def sum( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``sum`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -4422,6 +4525,7 @@ def sum( See Also -------- numpy.sum + dask.array.sum DataArray.sum :ref:`groupby` User guide on groupby operations. @@ -4466,6 +4570,7 @@ def std( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``std`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -4515,6 +4620,7 @@ def std( See Also -------- numpy.std + dask.array.std DataArray.std :ref:`groupby` User guide on groupby operations. @@ -4559,6 +4665,7 @@ def var( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``var`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -4608,6 +4715,7 @@ def var( See Also -------- numpy.var + dask.array.var DataArray.var :ref:`groupby` User guide on groupby operations. @@ -4648,6 +4756,7 @@ def median( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``median`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -4689,6 +4798,7 @@ def median( See Also -------- numpy.median + dask.array.median DataArray.median :ref:`groupby` User guide on groupby operations. @@ -4726,6 +4836,7 @@ def count( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``count`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -4759,6 +4870,7 @@ def count( See Also -------- numpy.count + dask.array.count DataArray.count :ref:`resampling` User guide on resampling operations. @@ -4791,6 +4903,7 @@ def all( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``all`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -4824,6 +4937,7 @@ def all( See Also -------- numpy.all + dask.array.all DataArray.all :ref:`resampling` User guide on resampling operations. @@ -4856,6 +4970,7 @@ def any( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``any`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -4889,6 +5004,7 @@ def any( See Also -------- numpy.any + dask.array.any DataArray.any :ref:`resampling` User guide on resampling operations. @@ -4927,6 +5043,7 @@ def max( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``max`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -4968,6 +5085,7 @@ def max( See Also -------- numpy.max + dask.array.max DataArray.max :ref:`resampling` User guide on resampling operations. @@ -5007,6 +5125,7 @@ def min( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``min`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -5048,6 +5167,7 @@ def min( See Also -------- numpy.min + dask.array.min DataArray.min :ref:`resampling` User guide on resampling operations. @@ -5087,6 +5207,7 @@ def mean( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``mean`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -5128,6 +5249,7 @@ def mean( See Also -------- numpy.mean + dask.array.mean DataArray.mean :ref:`resampling` User guide on resampling operations. @@ -5174,6 +5296,7 @@ def prod( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``prod`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -5223,6 +5346,7 @@ def prod( See Also -------- numpy.prod + dask.array.prod DataArray.prod :ref:`resampling` User guide on resampling operations. @@ -5270,6 +5394,7 @@ def sum( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``sum`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -5319,6 +5444,7 @@ def sum( See Also -------- numpy.sum + dask.array.sum DataArray.sum :ref:`resampling` User guide on resampling operations. @@ -5363,6 +5489,7 @@ def std( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``std`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -5412,6 +5539,7 @@ def std( See Also -------- numpy.std + dask.array.std DataArray.std :ref:`resampling` User guide on resampling operations. @@ -5456,6 +5584,7 @@ def var( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``var`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -5505,6 +5634,7 @@ def var( See Also -------- numpy.var + dask.array.var DataArray.var :ref:`resampling` User guide on resampling operations. @@ -5545,6 +5675,7 @@ def median( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``median`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -5586,6 +5717,7 @@ def median( See Also -------- numpy.median + dask.array.median DataArray.median :ref:`resampling` User guide on resampling operations. diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index 69d1c7db529..f35f7d99847 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -76,7 +76,7 @@ def {method}( -------- numpy.{method} dask.array.{method} - {obj}.{method} + {see_also_obj}.{method} :ref:`{docref}` User guide on {docref_description}. """''' @@ -181,6 +181,7 @@ def __init__( docref, docref_description, example_call_preamble, + see_also_obj=None, ): self.datastructure = datastructure self.cls = cls @@ -189,6 +190,10 @@ def __init__( self.docref_description = docref_description self.example_call_preamble = example_call_preamble self.preamble = CLASS_PREAMBLE.format(obj=datastructure.name, cls=cls) + if not see_also_obj: + self.see_also_obj = self.datastructure.name + else: + self.see_also_obj = see_also_obj def generate_methods(self): yield [self.preamble] @@ -227,6 +232,7 @@ def generate_method(self, method): **template_kwargs, docref=self.docref, docref_description=self.docref_description, + see_also_obj=self.see_also_obj, ) yield self.generate_code(method) @@ -319,6 +325,7 @@ class DataStructure: docref="agg", docref_description="reduction or aggregation operations", example_call_preamble="", + see_also_obj="DataArray", ) DataArrayGenerator = ClassReductionGenerator( cls="", @@ -327,6 +334,7 @@ class DataStructure: docref="agg", docref_description="reduction or aggregation operations", example_call_preamble="", + see_also_obj="Dataset", ) DataArrayGroupByGenerator = ClassReductionGenerator( From 99bfe128066ec3ef1b297650a47e2dd0a45801a8 Mon Sep 17 00:00:00 2001 From: dcherian Date: Mon, 8 Nov 2021 12:44:23 -0700 Subject: [PATCH 06/31] Fixes #5898 --- xarray/core/_reductions.py | 144 +++++++++++++++++++++++++++++ xarray/util/generate_reductions.py | 9 ++ 2 files changed, 153 insertions(+) diff --git a/xarray/core/_reductions.py b/xarray/core/_reductions.py index 1d6d4f88970..458910ac308 100644 --- a/xarray/core/_reductions.py +++ b/xarray/core/_reductions.py @@ -496,6 +496,10 @@ def mean( Data variables: da float64 nan + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.mean @@ -597,6 +601,10 @@ def prod( Data variables: da float64 12.0 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.prod @@ -699,6 +707,10 @@ def sum( Data variables: da float64 9.0 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.sum @@ -798,6 +810,10 @@ def std( Data variables: da float64 0.8367 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.std @@ -897,6 +913,10 @@ def var( Data variables: da float64 0.7 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.var @@ -984,6 +1004,10 @@ def median( Data variables: da float64 nan + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.median @@ -1418,6 +1442,10 @@ def mean( array(nan) + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.mean @@ -1509,6 +1537,10 @@ def prod( array(12.) + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.prod @@ -1601,6 +1633,10 @@ def sum( array(9.) + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.sum @@ -1690,6 +1726,10 @@ def std( array(0.83666003) + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.std @@ -1779,6 +1819,10 @@ def var( array(0.7) + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.var @@ -1858,6 +1902,10 @@ def median( array(nan) + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.median @@ -2350,6 +2398,10 @@ def mean( Data variables: da (labels) float64 nan 2.0 2.0 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.mean @@ -2457,6 +2509,10 @@ def prod( Data variables: da (labels) float64 nan 4.0 3.0 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.prod @@ -2565,6 +2621,10 @@ def sum( Data variables: da (labels) float64 nan 4.0 4.0 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.sum @@ -2670,6 +2730,10 @@ def std( Data variables: da (labels) float64 nan 0.0 1.414 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.std @@ -2775,6 +2839,10 @@ def var( Data variables: da (labels) float64 nan 0.0 2.0 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.var @@ -2866,6 +2934,10 @@ def median( Data variables: da (labels) float64 nan 2.0 2.0 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.median @@ -3359,6 +3431,10 @@ def mean( Data variables: da (time) float64 1.0 2.0 nan + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.mean @@ -3466,6 +3542,10 @@ def prod( Data variables: da (time) float64 nan 6.0 nan + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.prod @@ -3574,6 +3654,10 @@ def sum( Data variables: da (time) float64 nan 6.0 nan + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.sum @@ -3679,6 +3763,10 @@ def std( Data variables: da (time) float64 nan 1.0 nan + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.std @@ -3784,6 +3872,10 @@ def var( Data variables: da (time) float64 nan 1.0 nan + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.var @@ -3875,6 +3967,10 @@ def median( Data variables: da (time) float64 1.0 2.0 nan + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.median @@ -4327,6 +4423,10 @@ def mean( Coordinates: * labels (labels) object 'a' 'b' 'c' + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.mean @@ -4424,6 +4524,10 @@ def prod( Coordinates: * labels (labels) object 'a' 'b' 'c' + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.prod @@ -4522,6 +4626,10 @@ def sum( Coordinates: * labels (labels) object 'a' 'b' 'c' + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.sum @@ -4617,6 +4725,10 @@ def std( Coordinates: * labels (labels) object 'a' 'b' 'c' + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.std @@ -4712,6 +4824,10 @@ def var( Coordinates: * labels (labels) object 'a' 'b' 'c' + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.var @@ -4795,6 +4911,10 @@ def median( Coordinates: * labels (labels) object 'a' 'b' 'c' + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.median @@ -5246,6 +5366,10 @@ def mean( Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.mean @@ -5343,6 +5467,10 @@ def prod( Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.prod @@ -5441,6 +5569,10 @@ def sum( Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.sum @@ -5536,6 +5668,10 @@ def std( Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.std @@ -5631,6 +5767,10 @@ def var( Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.var @@ -5714,6 +5854,10 @@ def median( Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Notes + ----- + Non-numeric variables will be removed prior to reducing. + See Also -------- numpy.median diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index f35f7d99847..c572a96dcf4 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -81,6 +81,11 @@ def {method}( User guide on {docref_description}. """''' +TEMPLATE_NOTES = """ + Notes + ----- + {notes}""" + _DIM_DOCSTRING = """dim : hashable or iterable of hashable, optional Name of dimension[s] along which to apply ``{method}``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions.""" @@ -118,6 +123,7 @@ def {method}( "cumsum", "cumprod", ] +_NUMERIC_ONLY_NOTES = "Non-numeric variables will be removed prior to reducing." extra_kwarg = collections.namedtuple("extra_kwarg", "docs kwarg call example") skipna = extra_kwarg( @@ -228,6 +234,9 @@ def generate_method(self, method): yield textwrap.indent(self.generate_example(method=method), "") + if method.numeric_only: + yield TEMPLATE_NOTES.format(notes=_NUMERIC_ONLY_NOTES) + yield TEMPLATE_SEE_ALSO.format( **template_kwargs, docref=self.docref, From 9bb2c321e8df2f5978c40a1c3c8f891c77e847ff Mon Sep 17 00:00:00 2001 From: dcherian Date: Mon, 8 Nov 2021 13:56:53 -0700 Subject: [PATCH 07/31] Reorder docstring to match numpy --- xarray/util/generate_reductions.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index c572a96dcf4..f9a2d5e2db9 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -71,15 +71,14 @@ def {method}( New {obj} with ``{method}`` applied to its data and the indicated dimension(s) removed""" -TEMPLATE_SEE_ALSO = ''' +TEMPLATE_SEE_ALSO = """ See Also -------- numpy.{method} dask.array.{method} {see_also_obj}.{method} :ref:`{docref}` - User guide on {docref_description}. - """''' + User guide on {docref_description}.""" TEMPLATE_NOTES = """ Notes @@ -232,11 +231,6 @@ def generate_method(self, method): yield TEMPLATE_RETURNS.format(**template_kwargs) - yield textwrap.indent(self.generate_example(method=method), "") - - if method.numeric_only: - yield TEMPLATE_NOTES.format(notes=_NUMERIC_ONLY_NOTES) - yield TEMPLATE_SEE_ALSO.format( **template_kwargs, docref=self.docref, @@ -244,6 +238,13 @@ def generate_method(self, method): see_also_obj=self.see_also_obj, ) + if method.numeric_only: + yield TEMPLATE_NOTES.format(notes=_NUMERIC_ONLY_NOTES) + + yield textwrap.indent(self.generate_example(method=method), "") + + yield ' """' + yield self.generate_code(method) def generate_example(self, method): From dea8fd9f326a543807c26b6a62e84b28b5cb4cc3 Mon Sep 17 00:00:00 2001 From: dcherian Date: Mon, 8 Nov 2021 16:18:07 -0700 Subject: [PATCH 08/31] REfactor --- xarray/core/_reductions.py | 1344 ++++++++++++++-------------- xarray/util/generate_reductions.py | 16 +- 2 files changed, 681 insertions(+), 679 deletions(-) diff --git a/xarray/core/_reductions.py b/xarray/core/_reductions.py index 458910ac308..006f4e35a09 100644 --- a/xarray/core/_reductions.py +++ b/xarray/core/_reductions.py @@ -71,6 +71,14 @@ def count( New Dataset with ``count`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.count + dask.array.count + DataArray.count + :ref:`agg` + User guide on reduction or aggregation operations. + Examples -------- >>> da = xr.DataArray( @@ -96,14 +104,6 @@ def count( Dimensions: () Data variables: da int64 5 - - See Also - -------- - numpy.count - dask.array.count - DataArray.count - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.count, @@ -142,6 +142,14 @@ def all( New Dataset with ``all`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.all + dask.array.all + DataArray.all + :ref:`agg` + User guide on reduction or aggregation operations. + Examples -------- >>> da = xr.DataArray( @@ -167,14 +175,6 @@ def all( Dimensions: () Data variables: da bool False - - See Also - -------- - numpy.all - dask.array.all - DataArray.all - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.array_all, @@ -213,6 +213,14 @@ def any( New Dataset with ``any`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.any + dask.array.any + DataArray.any + :ref:`agg` + User guide on reduction or aggregation operations. + Examples -------- >>> da = xr.DataArray( @@ -238,14 +246,6 @@ def any( Dimensions: () Data variables: da bool True - - See Also - -------- - numpy.any - dask.array.any - DataArray.any - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.array_any, @@ -290,6 +290,14 @@ def max( New Dataset with ``max`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.max + dask.array.max + DataArray.max + :ref:`agg` + User guide on reduction or aggregation operations. + Examples -------- >>> da = xr.DataArray( @@ -323,14 +331,6 @@ def max( Dimensions: () Data variables: da float64 nan - - See Also - -------- - numpy.max - dask.array.max - DataArray.max - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.max, @@ -376,6 +376,14 @@ def min( New Dataset with ``min`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.min + dask.array.min + DataArray.min + :ref:`agg` + User guide on reduction or aggregation operations. + Examples -------- >>> da = xr.DataArray( @@ -409,14 +417,6 @@ def min( Dimensions: () Data variables: da float64 nan - - See Also - -------- - numpy.min - dask.array.min - DataArray.min - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.min, @@ -462,6 +462,18 @@ def mean( New Dataset with ``mean`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.mean + dask.array.mean + DataArray.mean + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -495,18 +507,6 @@ def mean( Dimensions: () Data variables: da float64 nan - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.mean - dask.array.mean - DataArray.mean - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.mean, @@ -559,6 +559,18 @@ def prod( New Dataset with ``prod`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.prod + dask.array.prod + DataArray.prod + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -600,18 +612,6 @@ def prod( Dimensions: () Data variables: da float64 12.0 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.prod - dask.array.prod - DataArray.prod - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.prod, @@ -665,6 +665,18 @@ def sum( New Dataset with ``sum`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.sum + dask.array.sum + DataArray.sum + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -706,18 +718,6 @@ def sum( Dimensions: () Data variables: da float64 9.0 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.sum - dask.array.sum - DataArray.sum - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.sum, @@ -768,6 +768,18 @@ def std( New Dataset with ``std`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.std + dask.array.std + DataArray.std + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -809,18 +821,6 @@ def std( Dimensions: () Data variables: da float64 0.8367 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.std - dask.array.std - DataArray.std - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.std, @@ -871,6 +871,18 @@ def var( New Dataset with ``var`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.var + dask.array.var + DataArray.var + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -912,18 +924,6 @@ def var( Dimensions: () Data variables: da float64 0.7 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.var - dask.array.var - DataArray.var - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.var, @@ -970,6 +970,18 @@ def median( New Dataset with ``median`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.median + dask.array.median + DataArray.median + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -1003,18 +1015,6 @@ def median( Dimensions: () Data variables: da float64 nan - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.median - dask.array.median - DataArray.median - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.median, @@ -1058,6 +1058,14 @@ def count( New DataArray with ``count`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.count + dask.array.count + Dataset.count + :ref:`agg` + User guide on reduction or aggregation operations. + Examples -------- >>> da = xr.DataArray( @@ -1078,14 +1086,6 @@ def count( >>> da.count() array(5) - - See Also - -------- - numpy.count - dask.array.count - Dataset.count - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.count, @@ -1123,6 +1123,14 @@ def all( New DataArray with ``all`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.all + dask.array.all + Dataset.all + :ref:`agg` + User guide on reduction or aggregation operations. + Examples -------- >>> da = xr.DataArray( @@ -1143,14 +1151,6 @@ def all( >>> da.all() array(False) - - See Also - -------- - numpy.all - dask.array.all - Dataset.all - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.array_all, @@ -1188,6 +1188,14 @@ def any( New DataArray with ``any`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.any + dask.array.any + Dataset.any + :ref:`agg` + User guide on reduction or aggregation operations. + Examples -------- >>> da = xr.DataArray( @@ -1208,14 +1216,6 @@ def any( >>> da.any() array(True) - - See Also - -------- - numpy.any - dask.array.any - Dataset.any - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.array_any, @@ -1259,6 +1259,14 @@ def max( New DataArray with ``max`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.max + dask.array.max + Dataset.max + :ref:`agg` + User guide on reduction or aggregation operations. + Examples -------- >>> da = xr.DataArray( @@ -1285,14 +1293,6 @@ def max( >>> da.max(skipna=False) array(nan) - - See Also - -------- - numpy.max - dask.array.max - Dataset.max - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.max, @@ -1337,6 +1337,14 @@ def min( New DataArray with ``min`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.min + dask.array.min + Dataset.min + :ref:`agg` + User guide on reduction or aggregation operations. + Examples -------- >>> da = xr.DataArray( @@ -1363,14 +1371,6 @@ def min( >>> da.min(skipna=False) array(nan) - - See Also - -------- - numpy.min - dask.array.min - Dataset.min - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.min, @@ -1415,6 +1415,18 @@ def mean( New DataArray with ``mean`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.mean + dask.array.mean + Dataset.mean + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -1441,18 +1453,6 @@ def mean( >>> da.mean(skipna=False) array(nan) - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.mean - dask.array.mean - Dataset.mean - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.mean, @@ -1504,6 +1504,18 @@ def prod( New DataArray with ``prod`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.prod + dask.array.prod + Dataset.prod + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -1536,18 +1548,6 @@ def prod( >>> da.prod(skipna=True, min_count=2) array(12.) - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.prod - dask.array.prod - Dataset.prod - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.prod, @@ -1600,6 +1600,18 @@ def sum( New DataArray with ``sum`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.sum + dask.array.sum + Dataset.sum + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -1632,18 +1644,6 @@ def sum( >>> da.sum(skipna=True, min_count=2) array(9.) - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.sum - dask.array.sum - Dataset.sum - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.sum, @@ -1693,6 +1693,18 @@ def std( New DataArray with ``std`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.std + dask.array.std + Dataset.std + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -1725,18 +1737,6 @@ def std( >>> da.std(skipna=True, ddof=1) array(0.83666003) - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.std - dask.array.std - Dataset.std - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.std, @@ -1786,6 +1786,18 @@ def var( New DataArray with ``var`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.var + dask.array.var + Dataset.var + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -1818,18 +1830,6 @@ def var( >>> da.var(skipna=True, ddof=1) array(0.7) - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.var - dask.array.var - Dataset.var - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.var, @@ -1875,6 +1875,18 @@ def median( New DataArray with ``median`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.median + dask.array.median + Dataset.median + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -1901,18 +1913,6 @@ def median( >>> da.median(skipna=False) array(nan) - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.median - dask.array.median - Dataset.median - :ref:`agg` - User guide on reduction or aggregation operations. """ return self.reduce( duck_array_ops.median, @@ -1955,6 +1955,14 @@ def count( New Dataset with ``count`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.count + dask.array.count + Dataset.count + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -1982,14 +1990,6 @@ def count( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) int64 1 2 2 - - See Also - -------- - numpy.count - dask.array.count - Dataset.count - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.count, @@ -2028,6 +2028,14 @@ def all( New Dataset with ``all`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.all + dask.array.all + Dataset.all + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -2055,14 +2063,6 @@ def all( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) bool False True True - - See Also - -------- - numpy.all - dask.array.all - Dataset.all - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.array_all, @@ -2101,6 +2101,14 @@ def any( New Dataset with ``any`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.any + dask.array.any + Dataset.any + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -2128,14 +2136,6 @@ def any( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) bool True True True - - See Also - -------- - numpy.any - dask.array.any - Dataset.any - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.array_any, @@ -2180,6 +2180,14 @@ def max( New Dataset with ``max`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.max + dask.array.max + Dataset.max + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -2217,14 +2225,6 @@ def max( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) float64 nan 2.0 3.0 - - See Also - -------- - numpy.max - dask.array.max - Dataset.max - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.max, @@ -2270,6 +2270,14 @@ def min( New Dataset with ``min`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.min + dask.array.min + Dataset.min + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -2307,14 +2315,6 @@ def min( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) float64 nan 2.0 1.0 - - See Also - -------- - numpy.min - dask.array.min - Dataset.min - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.min, @@ -2360,6 +2360,18 @@ def mean( New Dataset with ``mean`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.mean + dask.array.mean + Dataset.mean + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -2397,18 +2409,6 @@ def mean( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) float64 nan 2.0 2.0 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.mean - dask.array.mean - Dataset.mean - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.mean, @@ -2461,6 +2461,18 @@ def prod( New Dataset with ``prod`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.prod + dask.array.prod + Dataset.prod + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -2508,18 +2520,6 @@ def prod( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) float64 nan 4.0 3.0 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.prod - dask.array.prod - Dataset.prod - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.prod, @@ -2573,6 +2573,18 @@ def sum( New Dataset with ``sum`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.sum + dask.array.sum + Dataset.sum + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -2620,18 +2632,6 @@ def sum( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) float64 nan 4.0 4.0 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.sum - dask.array.sum - Dataset.sum - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.sum, @@ -2682,6 +2682,18 @@ def std( New Dataset with ``std`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.std + dask.array.std + Dataset.std + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -2729,18 +2741,6 @@ def std( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) float64 nan 0.0 1.414 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.std - dask.array.std - Dataset.std - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.std, @@ -2791,6 +2791,18 @@ def var( New Dataset with ``var`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.var + dask.array.var + Dataset.var + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -2838,18 +2850,6 @@ def var( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) float64 nan 0.0 2.0 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.var - dask.array.var - Dataset.var - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.var, @@ -2896,6 +2896,18 @@ def median( New Dataset with ``median`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.median + dask.array.median + Dataset.median + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -2933,18 +2945,6 @@ def median( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) float64 nan 2.0 2.0 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.median - dask.array.median - Dataset.median - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.median, @@ -2988,6 +2988,14 @@ def count( New Dataset with ``count`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.count + dask.array.count + Dataset.count + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -3015,14 +3023,6 @@ def count( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) int64 1 3 1 - - See Also - -------- - numpy.count - dask.array.count - Dataset.count - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.count, @@ -3061,6 +3061,14 @@ def all( New Dataset with ``all`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.all + dask.array.all + Dataset.all + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -3088,14 +3096,6 @@ def all( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) bool True True False - - See Also - -------- - numpy.all - dask.array.all - Dataset.all - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.array_all, @@ -3134,6 +3134,14 @@ def any( New Dataset with ``any`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.any + dask.array.any + Dataset.any + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -3161,14 +3169,6 @@ def any( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) bool True True True - - See Also - -------- - numpy.any - dask.array.any - Dataset.any - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.array_any, @@ -3213,6 +3213,14 @@ def max( New Dataset with ``max`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.max + dask.array.max + Dataset.max + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -3250,14 +3258,6 @@ def max( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) float64 1.0 3.0 nan - - See Also - -------- - numpy.max - dask.array.max - Dataset.max - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.max, @@ -3303,6 +3303,14 @@ def min( New Dataset with ``min`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.min + dask.array.min + Dataset.min + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -3340,14 +3348,6 @@ def min( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) float64 1.0 1.0 nan - - See Also - -------- - numpy.min - dask.array.min - Dataset.min - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.min, @@ -3393,6 +3393,18 @@ def mean( New Dataset with ``mean`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.mean + dask.array.mean + Dataset.mean + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -3430,18 +3442,6 @@ def mean( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) float64 1.0 2.0 nan - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.mean - dask.array.mean - Dataset.mean - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.mean, @@ -3494,6 +3494,18 @@ def prod( New Dataset with ``prod`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.prod + dask.array.prod + Dataset.prod + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -3541,18 +3553,6 @@ def prod( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) float64 nan 6.0 nan - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.prod - dask.array.prod - Dataset.prod - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.prod, @@ -3606,6 +3606,18 @@ def sum( New Dataset with ``sum`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.sum + dask.array.sum + Dataset.sum + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -3653,18 +3665,6 @@ def sum( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) float64 nan 6.0 nan - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.sum - dask.array.sum - Dataset.sum - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.sum, @@ -3715,6 +3715,18 @@ def std( New Dataset with ``std`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.std + dask.array.std + Dataset.std + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -3762,18 +3774,6 @@ def std( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) float64 nan 1.0 nan - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.std - dask.array.std - Dataset.std - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.std, @@ -3824,6 +3824,18 @@ def var( New Dataset with ``var`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.var + dask.array.var + Dataset.var + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -3871,18 +3883,6 @@ def var( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) float64 nan 1.0 nan - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.var - dask.array.var - Dataset.var - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.var, @@ -3929,6 +3929,18 @@ def median( New Dataset with ``median`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.median + dask.array.median + Dataset.median + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -3966,18 +3978,6 @@ def median( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) float64 1.0 2.0 nan - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.median - dask.array.median - Dataset.median - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.median, @@ -4021,6 +4021,14 @@ def count( New DataArray with ``count`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.count + dask.array.count + DataArray.count + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -4043,14 +4051,6 @@ def count( array([1, 2, 2]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - See Also - -------- - numpy.count - dask.array.count - DataArray.count - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.count, @@ -4088,6 +4088,14 @@ def all( New DataArray with ``all`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.all + dask.array.all + DataArray.all + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -4110,14 +4118,6 @@ def all( array([False, True, True]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - See Also - -------- - numpy.all - dask.array.all - DataArray.all - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.array_all, @@ -4155,6 +4155,14 @@ def any( New DataArray with ``any`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.any + dask.array.any + DataArray.any + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -4177,14 +4185,6 @@ def any( array([ True, True, True]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - See Also - -------- - numpy.any - dask.array.any - DataArray.any - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.array_any, @@ -4228,6 +4228,14 @@ def max( New DataArray with ``max`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.max + dask.array.max + DataArray.max + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -4258,14 +4266,6 @@ def max( array([nan, 2., 3.]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - See Also - -------- - numpy.max - dask.array.max - DataArray.max - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.max, @@ -4310,6 +4310,14 @@ def min( New DataArray with ``min`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.min + dask.array.min + DataArray.min + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -4340,14 +4348,6 @@ def min( array([nan, 2., 1.]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - See Also - -------- - numpy.min - dask.array.min - DataArray.min - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.min, @@ -4392,6 +4392,18 @@ def mean( New DataArray with ``mean`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.mean + dask.array.mean + DataArray.mean + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -4422,18 +4434,6 @@ def mean( array([nan, 2., 2.]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.mean - dask.array.mean - DataArray.mean - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.mean, @@ -4485,6 +4485,18 @@ def prod( New DataArray with ``prod`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.prod + dask.array.prod + DataArray.prod + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -4523,18 +4535,6 @@ def prod( array([nan, 4., 3.]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.prod - dask.array.prod - DataArray.prod - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.prod, @@ -4587,6 +4587,18 @@ def sum( New DataArray with ``sum`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.sum + dask.array.sum + DataArray.sum + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -4625,18 +4637,6 @@ def sum( array([nan, 4., 4.]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.sum - dask.array.sum - DataArray.sum - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.sum, @@ -4686,6 +4686,18 @@ def std( New DataArray with ``std`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.std + dask.array.std + DataArray.std + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -4724,18 +4736,6 @@ def std( array([ nan, 0. , 1.41421356]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.std - dask.array.std - DataArray.std - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.std, @@ -4785,6 +4785,18 @@ def var( New DataArray with ``var`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.var + dask.array.var + DataArray.var + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -4823,18 +4835,6 @@ def var( array([nan, 0., 2.]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.var - dask.array.var - DataArray.var - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.var, @@ -4880,6 +4880,18 @@ def median( New DataArray with ``median`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.median + dask.array.median + DataArray.median + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -4910,18 +4922,6 @@ def median( array([nan, 2., 2.]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.median - dask.array.median - DataArray.median - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.median, @@ -4964,6 +4964,14 @@ def count( New DataArray with ``count`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.count + dask.array.count + DataArray.count + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -4986,14 +4994,6 @@ def count( array([1, 3, 1]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - See Also - -------- - numpy.count - dask.array.count - DataArray.count - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.count, @@ -5031,6 +5031,14 @@ def all( New DataArray with ``all`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.all + dask.array.all + DataArray.all + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -5053,14 +5061,6 @@ def all( array([ True, True, False]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - See Also - -------- - numpy.all - dask.array.all - DataArray.all - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.array_all, @@ -5098,6 +5098,14 @@ def any( New DataArray with ``any`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.any + dask.array.any + DataArray.any + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -5120,14 +5128,6 @@ def any( array([ True, True, True]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - See Also - -------- - numpy.any - dask.array.any - DataArray.any - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.array_any, @@ -5171,6 +5171,14 @@ def max( New DataArray with ``max`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.max + dask.array.max + DataArray.max + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -5201,14 +5209,6 @@ def max( array([ 1., 3., nan]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - See Also - -------- - numpy.max - dask.array.max - DataArray.max - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.max, @@ -5253,6 +5253,14 @@ def min( New DataArray with ``min`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.min + dask.array.min + DataArray.min + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -5283,14 +5291,6 @@ def min( array([ 1., 1., nan]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - See Also - -------- - numpy.min - dask.array.min - DataArray.min - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.min, @@ -5335,6 +5335,18 @@ def mean( New DataArray with ``mean`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.mean + dask.array.mean + DataArray.mean + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -5365,18 +5377,6 @@ def mean( array([ 1., 2., nan]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.mean - dask.array.mean - DataArray.mean - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.mean, @@ -5428,6 +5428,18 @@ def prod( New DataArray with ``prod`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.prod + dask.array.prod + DataArray.prod + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -5466,18 +5478,6 @@ def prod( array([nan, 6., nan]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.prod - dask.array.prod - DataArray.prod - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.prod, @@ -5530,6 +5530,18 @@ def sum( New DataArray with ``sum`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.sum + dask.array.sum + DataArray.sum + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -5568,18 +5580,6 @@ def sum( array([nan, 6., nan]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.sum - dask.array.sum - DataArray.sum - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.sum, @@ -5629,6 +5629,18 @@ def std( New DataArray with ``std`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.std + dask.array.std + DataArray.std + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -5667,18 +5679,6 @@ def std( array([nan, 1., nan]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.std - dask.array.std - DataArray.std - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.std, @@ -5728,6 +5728,18 @@ def var( New DataArray with ``var`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.var + dask.array.var + DataArray.var + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -5766,18 +5778,6 @@ def var( array([nan, 1., nan]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.var - dask.array.var - DataArray.var - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.var, @@ -5823,6 +5823,18 @@ def median( New DataArray with ``median`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.median + dask.array.median + DataArray.median + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -5853,18 +5865,6 @@ def median( array([ 1., 2., nan]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - Notes - ----- - Non-numeric variables will be removed prior to reducing. - - See Also - -------- - numpy.median - dask.array.median - DataArray.median - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.median, diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index f9a2d5e2db9..fcb5a573c96 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -177,7 +177,7 @@ def __init__( ... np.array([1, 2, 3, 1, 2, np.nan]),""" -class ClassReductionGenerator: +class ReductionGenerator: def __init__( self, cls, @@ -271,6 +271,8 @@ def generate_example(self, method): >>> {calculation}(){extra_examples}""" + +class GenericReductionGenerator(ReductionGenerator): def generate_code(self, method): extra_kwargs = [kwarg.call for kwarg in method.extra_kwargs if kwarg.call] @@ -328,7 +330,7 @@ class DataStructure: numeric_only=False, ) -DatasetGenerator = ClassReductionGenerator( +DatasetGenerator = GenericReductionGenerator( cls="", datastructure=DatasetObject, methods=REDUCTION_METHODS, @@ -337,7 +339,7 @@ class DataStructure: example_call_preamble="", see_also_obj="DataArray", ) -DataArrayGenerator = ClassReductionGenerator( +DataArrayGenerator = GenericReductionGenerator( cls="", datastructure=DataArrayObject, methods=REDUCTION_METHODS, @@ -347,7 +349,7 @@ class DataStructure: see_also_obj="Dataset", ) -DataArrayGroupByGenerator = ClassReductionGenerator( +DataArrayGroupByGenerator = GenericReductionGenerator( cls="GroupBy", datastructure=DataArrayObject, methods=REDUCTION_METHODS, @@ -355,7 +357,7 @@ class DataStructure: docref_description="groupby operations", example_call_preamble='.groupby("labels")', ) -DataArrayResampleGenerator = ClassReductionGenerator( +DataArrayResampleGenerator = GenericReductionGenerator( cls="Resample", datastructure=DataArrayObject, methods=REDUCTION_METHODS, @@ -363,7 +365,7 @@ class DataStructure: docref_description="resampling operations", example_call_preamble='.resample(time="3M")', ) -DatasetGroupByGenerator = ClassReductionGenerator( +DatasetGroupByGenerator = GenericReductionGenerator( cls="GroupBy", datastructure=DatasetObject, methods=REDUCTION_METHODS, @@ -371,7 +373,7 @@ class DataStructure: docref_description="groupby operations", example_call_preamble='.groupby("labels")', ) -DatasetResampleGenerator = ClassReductionGenerator( +DatasetResampleGenerator = GenericReductionGenerator( cls="Resample", datastructure=DatasetObject, methods=REDUCTION_METHODS, From c7e9d9647f2c7df7e5b14644926dc2126ad4318f Mon Sep 17 00:00:00 2001 From: dcherian Date: Wed, 10 Nov 2021 11:49:47 -0700 Subject: [PATCH 09/31] Minor improvement --- xarray/util/generate_reductions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index fcb5a573c96..2ad65bab08b 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -280,7 +280,7 @@ def generate_code(self, method): extra_kwargs.append(f"numeric_only={method.numeric_only},") if extra_kwargs: - extra_kwargs = "\n " + "\n ".join(extra_kwargs) + extra_kwargs = textwrap.indent("\n" + "\n".join(extra_kwargs), 12 * " ") else: extra_kwargs = "" return f""" return self.reduce( From 5dcb5bfebe04302beb732259f3e805bd31691ed8 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 20 Nov 2021 12:30:50 +0100 Subject: [PATCH 10/31] Attempt fixing typing errors Mixing in DatasetReduce fixes: xarray/tests/test_groupby.py:460: error: Invalid self argument "Dataset" to attribute function "mean" with type "Callable[[DatasetReduce, Optional[Hashable], Optional[bool], Optional[bool], KwArg(Any)], T_Dataset]" [misc] Switching to "Dateset" as returned type fixes: xarray/tests/test_groupby.py:77: error: Need type annotation for "expected" [var-annotated] --- xarray/util/generate_reductions.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index 2ad65bab08b..ac3a1b500a0 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -21,15 +21,18 @@ # This file was generated using xarray.util.generate_reductions. Do not edit manually. import sys -from typing import Any, Callable, Hashable, Optional, Sequence, Union +from typing import TYPE_CHECKING, Any, Callable, Hashable, Optional, Sequence, Union from . import duck_array_ops -from .types import T_DataArray, T_Dataset if sys.version_info >= (3, 8): from typing import Protocol else: - from typing_extensions import Protocol''' + from typing_extensions import Protocol + +if TYPE_CHECKING: + from .dataset import Dataset + from.dataarray import DataArray''' OBJ_PREAMBLE = """ @@ -42,22 +45,22 @@ def reduce( keep_attrs: bool = None, keepdims: bool = False, **kwargs: Any, - ) -> T_{obj}: + ) -> "{obj}": ...""" CLASS_PREAMBLE = """ -class {obj}{cls}Reductions: +class {obj}{cls}Reductions({obj}Reduce): __slots__ = ()""" TEMPLATE_REDUCTION_SIGNATURE = ''' def {method}( - self: {obj}Reduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None,{extra_kwargs} keep_attrs: bool = None, **kwargs, - ) -> T_{obj}: + ) -> "{obj}": """ Reduce this {obj}'s data by applying ``{method}`` along some dimension(s). From 411d75d5ced18349968c060918e9bdcd4be04537 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 20 Nov 2021 18:00:08 +0100 Subject: [PATCH 11/31] Now get normal code running as well Protocols are not needed anymore when subclassing/defining directly in the class. When adding a dummy method in DatasetResampleReductions the order of subclassing had to be changed so the correct reduce was used. --- xarray/core/_reductions.py | 1347 ++++------------------------ xarray/core/resample.py | 4 +- xarray/util/generate_reductions.py | 25 +- 3 files changed, 204 insertions(+), 1172 deletions(-) diff --git a/xarray/core/_reductions.py b/xarray/core/_reductions.py index 006f4e35a09..e17b9dbd6b8 100644 --- a/xarray/core/_reductions.py +++ b/xarray/core/_reductions.py @@ -1,32 +1,18 @@ """Mixin classes with reduction operations.""" # This file was generated using xarray.util.generate_reductions. Do not edit manually. -import sys -from typing import Any, Callable, Hashable, Optional, Sequence, Union +from typing import TYPE_CHECKING, Any, Callable, Hashable, Optional, Sequence, Union from . import duck_array_ops -from .types import T_DataArray, T_Dataset -if sys.version_info >= (3, 8): - from typing import Protocol -else: - from typing_extensions import Protocol +if TYPE_CHECKING: + from .dataset import Dataset + from .dataarray import DataArray -class DatasetReduce(Protocol): - def reduce( - self, - func: Callable[..., Any], - dim: Union[None, Hashable, Sequence[Hashable]] = None, - axis: Union[None, int, Sequence[int]] = None, - keep_attrs: bool = None, - keepdims: bool = False, - **kwargs: Any, - ) -> T_Dataset: - ... - +class DatasetReductions: + __slots__ = () -class DataArrayReduce(Protocol): def reduce( self, func: Callable[..., Any], @@ -35,19 +21,15 @@ def reduce( keep_attrs: bool = None, keepdims: bool = False, **kwargs: Any, - ) -> T_DataArray: - ... - - -class DatasetReductions: - __slots__ = () + ) -> "Dataset": + return NotImplemented def count( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``count`` along some dimension(s). @@ -91,19 +73,8 @@ def count( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.count() - - Dimensions: () - Data variables: - da int64 5 """ return self.reduce( duck_array_ops.count, @@ -114,11 +85,11 @@ def count( ) def all( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``all`` along some dimension(s). @@ -162,19 +133,8 @@ def all( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.all() - - Dimensions: () - Data variables: - da bool False """ return self.reduce( duck_array_ops.array_all, @@ -185,11 +145,11 @@ def all( ) def any( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``any`` along some dimension(s). @@ -233,19 +193,8 @@ def any( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.any() - - Dimensions: () - Data variables: - da bool True """ return self.reduce( duck_array_ops.array_any, @@ -256,12 +205,12 @@ def any( ) def max( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``max`` along some dimension(s). @@ -310,27 +259,12 @@ def max( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.max() - - Dimensions: () - Data variables: - da float64 3.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.max(skipna=False) - - Dimensions: () - Data variables: - da float64 nan """ return self.reduce( duck_array_ops.max, @@ -342,12 +276,12 @@ def max( ) def min( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``min`` along some dimension(s). @@ -396,27 +330,12 @@ def min( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.min() - - Dimensions: () - Data variables: - da float64 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.min(skipna=False) - - Dimensions: () - Data variables: - da float64 nan """ return self.reduce( duck_array_ops.min, @@ -428,12 +347,12 @@ def min( ) def mean( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``mean`` along some dimension(s). @@ -486,27 +405,12 @@ def mean( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.mean() - - Dimensions: () - Data variables: - da float64 1.8 Use ``skipna`` to control whether NaNs are ignored. >>> ds.mean(skipna=False) - - Dimensions: () - Data variables: - da float64 nan """ return self.reduce( duck_array_ops.mean, @@ -518,13 +422,13 @@ def mean( ) def prod( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``prod`` along some dimension(s). @@ -583,35 +487,16 @@ def prod( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.prod() - - Dimensions: () - Data variables: - da float64 12.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.prod(skipna=False) - - Dimensions: () - Data variables: - da float64 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.prod(skipna=True, min_count=2) - - Dimensions: () - Data variables: - da float64 12.0 """ return self.reduce( duck_array_ops.prod, @@ -624,13 +509,13 @@ def prod( ) def sum( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``sum`` along some dimension(s). @@ -689,35 +574,16 @@ def sum( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.sum() - - Dimensions: () - Data variables: - da float64 9.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.sum(skipna=False) - - Dimensions: () - Data variables: - da float64 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.sum(skipna=True, min_count=2) - - Dimensions: () - Data variables: - da float64 9.0 """ return self.reduce( duck_array_ops.sum, @@ -730,13 +596,13 @@ def sum( ) def std( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``std`` along some dimension(s). @@ -792,35 +658,16 @@ def std( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.std() - - Dimensions: () - Data variables: - da float64 0.7483 Use ``skipna`` to control whether NaNs are ignored. >>> ds.std(skipna=False) - - Dimensions: () - Data variables: - da float64 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.std(skipna=True, ddof=1) - - Dimensions: () - Data variables: - da float64 0.8367 """ return self.reduce( duck_array_ops.std, @@ -833,13 +680,13 @@ def std( ) def var( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``var`` along some dimension(s). @@ -895,35 +742,16 @@ def var( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.var() - - Dimensions: () - Data variables: - da float64 0.56 Use ``skipna`` to control whether NaNs are ignored. >>> ds.var(skipna=False) - - Dimensions: () - Data variables: - da float64 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.var(skipna=True, ddof=1) - - Dimensions: () - Data variables: - da float64 0.7 """ return self.reduce( duck_array_ops.var, @@ -936,12 +764,12 @@ def var( ) def median( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``median`` along some dimension(s). @@ -994,27 +822,12 @@ def median( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.median() - - Dimensions: () - Data variables: - da float64 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.median(skipna=False) - - Dimensions: () - Data variables: - da float64 nan """ return self.reduce( duck_array_ops.median, @@ -1029,12 +842,23 @@ def median( class DataArrayReductions: __slots__ = () + def reduce( + self, + func: Callable[..., Any], + dim: Union[None, Hashable, Sequence[Hashable]] = None, + axis: Union[None, int, Sequence[int]] = None, + keep_attrs: bool = None, + keepdims: bool = False, + **kwargs: Any, + ) -> "DataArray": + return NotImplemented + def count( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``count`` along some dimension(s). @@ -1077,15 +901,8 @@ def count( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.count() - - array(5) """ return self.reduce( duck_array_ops.count, @@ -1095,11 +912,11 @@ def count( ) def all( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``all`` along some dimension(s). @@ -1142,15 +959,8 @@ def all( ... ), ... ) >>> da - - array([ True, True, True, True, True, False]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.all() - - array(False) """ return self.reduce( duck_array_ops.array_all, @@ -1160,11 +970,11 @@ def all( ) def any( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``any`` along some dimension(s). @@ -1207,15 +1017,8 @@ def any( ... ), ... ) >>> da - - array([ True, True, True, True, True, False]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.any() - - array(True) """ return self.reduce( duck_array_ops.array_any, @@ -1225,12 +1028,12 @@ def any( ) def max( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``max`` along some dimension(s). @@ -1278,21 +1081,12 @@ def max( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.max() - - array(3.) Use ``skipna`` to control whether NaNs are ignored. >>> da.max(skipna=False) - - array(nan) """ return self.reduce( duck_array_ops.max, @@ -1303,12 +1097,12 @@ def max( ) def min( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``min`` along some dimension(s). @@ -1356,21 +1150,12 @@ def min( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.min() - - array(1.) Use ``skipna`` to control whether NaNs are ignored. >>> da.min(skipna=False) - - array(nan) """ return self.reduce( duck_array_ops.min, @@ -1381,12 +1166,12 @@ def min( ) def mean( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``mean`` along some dimension(s). @@ -1438,21 +1223,12 @@ def mean( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.mean() - - array(1.8) Use ``skipna`` to control whether NaNs are ignored. >>> da.mean(skipna=False) - - array(nan) """ return self.reduce( duck_array_ops.mean, @@ -1463,13 +1239,13 @@ def mean( ) def prod( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``prod`` along some dimension(s). @@ -1527,27 +1303,16 @@ def prod( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.prod() - - array(12.) Use ``skipna`` to control whether NaNs are ignored. >>> da.prod(skipna=False) - - array(nan) Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.prod(skipna=True, min_count=2) - - array(12.) """ return self.reduce( duck_array_ops.prod, @@ -1559,13 +1324,13 @@ def prod( ) def sum( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``sum`` along some dimension(s). @@ -1623,27 +1388,16 @@ def sum( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.sum() - - array(9.) Use ``skipna`` to control whether NaNs are ignored. >>> da.sum(skipna=False) - - array(nan) Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.sum(skipna=True, min_count=2) - - array(9.) """ return self.reduce( duck_array_ops.sum, @@ -1655,13 +1409,13 @@ def sum( ) def std( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``std`` along some dimension(s). @@ -1716,27 +1470,16 @@ def std( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.std() - - array(0.74833148) Use ``skipna`` to control whether NaNs are ignored. >>> da.std(skipna=False) - - array(nan) Specify ``ddof=1`` for an unbiased estimate. >>> da.std(skipna=True, ddof=1) - - array(0.83666003) """ return self.reduce( duck_array_ops.std, @@ -1748,13 +1491,13 @@ def std( ) def var( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``var`` along some dimension(s). @@ -1809,27 +1552,16 @@ def var( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.var() - - array(0.56) Use ``skipna`` to control whether NaNs are ignored. >>> da.var(skipna=False) - - array(nan) Specify ``ddof=1`` for an unbiased estimate. >>> da.var(skipna=True, ddof=1) - - array(0.7) """ return self.reduce( duck_array_ops.var, @@ -1841,12 +1573,12 @@ def var( ) def median( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``median`` along some dimension(s). @@ -1898,21 +1630,12 @@ def median( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.median() - - array(2.) Use ``skipna`` to control whether NaNs are ignored. >>> da.median(skipna=False) - - array(nan) """ return self.reduce( duck_array_ops.median, @@ -1926,12 +1649,23 @@ def median( class DatasetGroupByReductions: __slots__ = () + def reduce( + self, + func: Callable[..., Any], + dim: Union[None, Hashable, Sequence[Hashable]] = None, + axis: Union[None, int, Sequence[int]] = None, + keep_attrs: bool = None, + keepdims: bool = False, + **kwargs: Any, + ) -> "Dataset": + return NotImplemented + def count( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``count`` along some dimension(s). @@ -1975,21 +1709,8 @@ def count( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").count() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) int64 1 2 2 """ return self.reduce( duck_array_ops.count, @@ -2000,11 +1721,11 @@ def count( ) def all( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``all`` along some dimension(s). @@ -2048,21 +1769,8 @@ def all( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").all() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) bool False True True """ return self.reduce( duck_array_ops.array_all, @@ -2073,11 +1781,11 @@ def all( ) def any( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``any`` along some dimension(s). @@ -2121,21 +1829,8 @@ def any( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").any() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) bool True True True """ return self.reduce( duck_array_ops.array_any, @@ -2146,12 +1841,12 @@ def any( ) def max( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``max`` along some dimension(s). @@ -2200,31 +1895,12 @@ def max( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").max() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 1.0 2.0 3.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").max(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 2.0 3.0 """ return self.reduce( duck_array_ops.max, @@ -2236,12 +1912,12 @@ def max( ) def min( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``min`` along some dimension(s). @@ -2290,31 +1966,12 @@ def min( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").min() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 1.0 2.0 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").min(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 2.0 1.0 """ return self.reduce( duck_array_ops.min, @@ -2326,12 +1983,12 @@ def min( ) def mean( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``mean`` along some dimension(s). @@ -2384,31 +2041,12 @@ def mean( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").mean() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").mean(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 2.0 2.0 """ return self.reduce( duck_array_ops.mean, @@ -2420,13 +2058,13 @@ def mean( ) def prod( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``prod`` along some dimension(s). @@ -2485,41 +2123,16 @@ def prod( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").prod() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 1.0 4.0 3.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").prod(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 4.0 3.0 Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.groupby("labels").prod(skipna=True, min_count=2) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 4.0 3.0 """ return self.reduce( duck_array_ops.prod, @@ -2532,13 +2145,13 @@ def prod( ) def sum( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``sum`` along some dimension(s). @@ -2597,41 +2210,16 @@ def sum( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").sum() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 1.0 4.0 4.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").sum(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 4.0 4.0 Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.groupby("labels").sum(skipna=True, min_count=2) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 4.0 4.0 """ return self.reduce( duck_array_ops.sum, @@ -2644,13 +2232,13 @@ def sum( ) def std( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``std`` along some dimension(s). @@ -2706,41 +2294,16 @@ def std( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").std() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 0.0 0.0 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").std(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 0.0 1.0 Specify ``ddof=1`` for an unbiased estimate. >>> ds.groupby("labels").std(skipna=True, ddof=1) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 0.0 1.414 """ return self.reduce( duck_array_ops.std, @@ -2753,13 +2316,13 @@ def std( ) def var( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``var`` along some dimension(s). @@ -2815,41 +2378,16 @@ def var( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").var() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 0.0 0.0 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").var(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 0.0 1.0 Specify ``ddof=1`` for an unbiased estimate. >>> ds.groupby("labels").var(skipna=True, ddof=1) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 0.0 2.0 """ return self.reduce( duck_array_ops.var, @@ -2862,12 +2400,12 @@ def var( ) def median( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``median`` along some dimension(s). @@ -2920,31 +2458,12 @@ def median( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").median() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").median(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 2.0 2.0 """ return self.reduce( duck_array_ops.median, @@ -2959,12 +2478,23 @@ def median( class DatasetResampleReductions: __slots__ = () + def reduce( + self, + func: Callable[..., Any], + dim: Union[None, Hashable, Sequence[Hashable]] = None, + axis: Union[None, int, Sequence[int]] = None, + keep_attrs: bool = None, + keepdims: bool = False, + **kwargs: Any, + ) -> "Dataset": + return NotImplemented + def count( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``count`` along some dimension(s). @@ -3008,21 +2538,8 @@ def count( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").count() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) int64 1 3 1 """ return self.reduce( duck_array_ops.count, @@ -3033,11 +2550,11 @@ def count( ) def all( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``all`` along some dimension(s). @@ -3081,21 +2598,8 @@ def all( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").all() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) bool True True False """ return self.reduce( duck_array_ops.array_all, @@ -3106,11 +2610,11 @@ def all( ) def any( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``any`` along some dimension(s). @@ -3154,21 +2658,8 @@ def any( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").any() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) bool True True True """ return self.reduce( duck_array_ops.array_any, @@ -3179,12 +2670,12 @@ def any( ) def max( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``max`` along some dimension(s). @@ -3233,31 +2724,12 @@ def max( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").max() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 3.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").max(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 3.0 nan """ return self.reduce( duck_array_ops.max, @@ -3269,12 +2741,12 @@ def max( ) def min( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``min`` along some dimension(s). @@ -3323,31 +2795,12 @@ def min( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").min() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 1.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").min(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 1.0 nan """ return self.reduce( duck_array_ops.min, @@ -3359,12 +2812,12 @@ def min( ) def mean( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``mean`` along some dimension(s). @@ -3417,31 +2870,12 @@ def mean( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").mean() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").mean(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 2.0 nan """ return self.reduce( duck_array_ops.mean, @@ -3453,13 +2887,13 @@ def mean( ) def prod( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``prod`` along some dimension(s). @@ -3518,41 +2952,16 @@ def prod( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").prod() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 6.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").prod(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 6.0 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.resample(time="3M").prod(skipna=True, min_count=2) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 nan 6.0 nan """ return self.reduce( duck_array_ops.prod, @@ -3565,13 +2974,13 @@ def prod( ) def sum( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``sum`` along some dimension(s). @@ -3630,41 +3039,16 @@ def sum( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").sum() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 6.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").sum(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 6.0 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.resample(time="3M").sum(skipna=True, min_count=2) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 nan 6.0 nan """ return self.reduce( duck_array_ops.sum, @@ -3677,13 +3061,13 @@ def sum( ) def std( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``std`` along some dimension(s). @@ -3739,41 +3123,16 @@ def std( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").std() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 0.0 0.8165 0.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").std(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 0.0 0.8165 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.resample(time="3M").std(skipna=True, ddof=1) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 nan 1.0 nan """ return self.reduce( duck_array_ops.std, @@ -3786,13 +3145,13 @@ def std( ) def var( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``var`` along some dimension(s). @@ -3848,41 +3207,16 @@ def var( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").var() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 0.0 0.6667 0.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").var(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 0.0 0.6667 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.resample(time="3M").var(skipna=True, ddof=1) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 nan 1.0 nan """ return self.reduce( duck_array_ops.var, @@ -3895,12 +3229,12 @@ def var( ) def median( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``median`` along some dimension(s). @@ -3953,31 +3287,12 @@ def median( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").median() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").median(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 2.0 nan """ return self.reduce( duck_array_ops.median, @@ -3992,12 +3307,23 @@ def median( class DataArrayGroupByReductions: __slots__ = () + def reduce( + self, + func: Callable[..., Any], + dim: Union[None, Hashable, Sequence[Hashable]] = None, + axis: Union[None, int, Sequence[int]] = None, + keep_attrs: bool = None, + keepdims: bool = False, + **kwargs: Any, + ) -> "DataArray": + return NotImplemented + def count( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``count`` along some dimension(s). @@ -4040,17 +3366,8 @@ def count( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").count() - - array([1, 2, 2]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.count, @@ -4060,11 +3377,11 @@ def count( ) def all( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``all`` along some dimension(s). @@ -4107,17 +3424,8 @@ def all( ... ), ... ) >>> da - - array([ True, True, True, True, True, False]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").all() - - array([False, True, True]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.array_all, @@ -4127,11 +3435,11 @@ def all( ) def any( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``any`` along some dimension(s). @@ -4174,17 +3482,8 @@ def any( ... ), ... ) >>> da - - array([ True, True, True, True, True, False]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").any() - - array([ True, True, True]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.array_any, @@ -4194,12 +3493,12 @@ def any( ) def max( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``max`` along some dimension(s). @@ -4247,25 +3546,12 @@ def max( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").max() - - array([1., 2., 3.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").max(skipna=False) - - array([nan, 2., 3.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.max, @@ -4276,12 +3562,12 @@ def max( ) def min( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``min`` along some dimension(s). @@ -4329,25 +3615,12 @@ def min( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").min() - - array([1., 2., 1.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").min(skipna=False) - - array([nan, 2., 1.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.min, @@ -4358,12 +3631,12 @@ def min( ) def mean( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``mean`` along some dimension(s). @@ -4415,25 +3688,12 @@ def mean( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").mean() - - array([1., 2., 2.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").mean(skipna=False) - - array([nan, 2., 2.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.mean, @@ -4444,13 +3704,13 @@ def mean( ) def prod( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``prod`` along some dimension(s). @@ -4508,33 +3768,16 @@ def prod( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").prod() - - array([1., 4., 3.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").prod(skipna=False) - - array([nan, 4., 3.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.groupby("labels").prod(skipna=True, min_count=2) - - array([nan, 4., 3.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.prod, @@ -4546,13 +3789,13 @@ def prod( ) def sum( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``sum`` along some dimension(s). @@ -4610,33 +3853,16 @@ def sum( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").sum() - - array([1., 4., 4.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").sum(skipna=False) - - array([nan, 4., 4.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.groupby("labels").sum(skipna=True, min_count=2) - - array([nan, 4., 4.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.sum, @@ -4648,13 +3874,13 @@ def sum( ) def std( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``std`` along some dimension(s). @@ -4709,33 +3935,16 @@ def std( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").std() - - array([0., 0., 1.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").std(skipna=False) - - array([nan, 0., 1.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Specify ``ddof=1`` for an unbiased estimate. >>> da.groupby("labels").std(skipna=True, ddof=1) - - array([ nan, 0. , 1.41421356]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.std, @@ -4747,13 +3956,13 @@ def std( ) def var( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``var`` along some dimension(s). @@ -4808,33 +4017,16 @@ def var( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").var() - - array([0., 0., 1.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").var(skipna=False) - - array([nan, 0., 1.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Specify ``ddof=1`` for an unbiased estimate. >>> da.groupby("labels").var(skipna=True, ddof=1) - - array([nan, 0., 2.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.var, @@ -4846,12 +4038,12 @@ def var( ) def median( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``median`` along some dimension(s). @@ -4903,25 +4095,12 @@ def median( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").median() - - array([1., 2., 2.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").median(skipna=False) - - array([nan, 2., 2.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.median, @@ -4935,12 +4114,23 @@ def median( class DataArrayResampleReductions: __slots__ = () + def reduce( + self, + func: Callable[..., Any], + dim: Union[None, Hashable, Sequence[Hashable]] = None, + axis: Union[None, int, Sequence[int]] = None, + keep_attrs: bool = None, + keepdims: bool = False, + **kwargs: Any, + ) -> "DataArray": + return NotImplemented + def count( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``count`` along some dimension(s). @@ -4983,17 +4173,8 @@ def count( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").count() - - array([1, 3, 1]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.count, @@ -5003,11 +4184,11 @@ def count( ) def all( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``all`` along some dimension(s). @@ -5050,17 +4231,8 @@ def all( ... ), ... ) >>> da - - array([ True, True, True, True, True, False]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").all() - - array([ True, True, False]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.array_all, @@ -5070,11 +4242,11 @@ def all( ) def any( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``any`` along some dimension(s). @@ -5117,17 +4289,8 @@ def any( ... ), ... ) >>> da - - array([ True, True, True, True, True, False]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").any() - - array([ True, True, True]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.array_any, @@ -5137,12 +4300,12 @@ def any( ) def max( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``max`` along some dimension(s). @@ -5190,25 +4353,12 @@ def max( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").max() - - array([1., 3., 2.]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").max(skipna=False) - - array([ 1., 3., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.max, @@ -5219,12 +4369,12 @@ def max( ) def min( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``min`` along some dimension(s). @@ -5272,25 +4422,12 @@ def min( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").min() - - array([1., 1., 2.]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").min(skipna=False) - - array([ 1., 1., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.min, @@ -5301,12 +4438,12 @@ def min( ) def mean( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``mean`` along some dimension(s). @@ -5358,25 +4495,12 @@ def mean( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").mean() - - array([1., 2., 2.]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").mean(skipna=False) - - array([ 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.mean, @@ -5387,13 +4511,13 @@ def mean( ) def prod( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``prod`` along some dimension(s). @@ -5451,33 +4575,16 @@ def prod( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").prod() - - array([1., 6., 2.]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").prod(skipna=False) - - array([ 1., 6., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.resample(time="3M").prod(skipna=True, min_count=2) - - array([nan, 6., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.prod, @@ -5489,13 +4596,13 @@ def prod( ) def sum( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``sum`` along some dimension(s). @@ -5553,33 +4660,16 @@ def sum( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").sum() - - array([1., 6., 2.]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").sum(skipna=False) - - array([ 1., 6., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.resample(time="3M").sum(skipna=True, min_count=2) - - array([nan, 6., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.sum, @@ -5591,13 +4681,13 @@ def sum( ) def std( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``std`` along some dimension(s). @@ -5652,33 +4742,16 @@ def std( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").std() - - array([0. , 0.81649658, 0. ]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").std(skipna=False) - - array([0. , 0.81649658, nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``ddof=1`` for an unbiased estimate. >>> da.resample(time="3M").std(skipna=True, ddof=1) - - array([nan, 1., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.std, @@ -5690,13 +4763,13 @@ def std( ) def var( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``var`` along some dimension(s). @@ -5751,33 +4824,16 @@ def var( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").var() - - array([0. , 0.66666667, 0. ]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").var(skipna=False) - - array([0. , 0.66666667, nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``ddof=1`` for an unbiased estimate. >>> da.resample(time="3M").var(skipna=True, ddof=1) - - array([nan, 1., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.var, @@ -5789,12 +4845,12 @@ def var( ) def median( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``median`` along some dimension(s). @@ -5846,25 +4902,12 @@ def median( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").median() - - array([1., 2., 2.]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").median(skipna=False) - - array([ 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.median, diff --git a/xarray/core/resample.py b/xarray/core/resample.py index e2f599e8b4e..ad11550372c 100644 --- a/xarray/core/resample.py +++ b/xarray/core/resample.py @@ -157,7 +157,7 @@ def _interpolate(self, kind="linear"): ) -class DataArrayResample(DataArrayResampleReductions, DataArrayGroupByBase, Resample): +class DataArrayResample(DataArrayGroupByBase, DataArrayResampleReductions, Resample): """DataArrayGroupBy object specialized to time resampling operations over a specified dimension """ @@ -248,7 +248,7 @@ def apply(self, func, args=(), shortcut=None, **kwargs): return self.map(func=func, shortcut=shortcut, args=args, **kwargs) -class DatasetResample(DatasetResampleReductions, DatasetGroupByBase, Resample): +class DatasetResample(DatasetGroupByBase, DatasetResampleReductions, Resample): """DatasetGroupBy object specialized to resampling a specified dimension""" def __init__(self, *args, dim=None, resample_dim=None, **kwargs): diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index ac3a1b500a0..ea479ccc9c3 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -20,23 +20,20 @@ """Mixin classes with reduction operations.""" # This file was generated using xarray.util.generate_reductions. Do not edit manually. -import sys from typing import TYPE_CHECKING, Any, Callable, Hashable, Optional, Sequence, Union from . import duck_array_ops -if sys.version_info >= (3, 8): - from typing import Protocol -else: - from typing_extensions import Protocol - if TYPE_CHECKING: from .dataset import Dataset - from.dataarray import DataArray''' + from .dataarray import DataArray''' + + +CLASS_PREAMBLE = """ -OBJ_PREAMBLE = """ +class {obj}{cls}Reductions: + __slots__ = () -class {obj}Reduce(Protocol): def reduce( self, func: Callable[..., Any], @@ -46,13 +43,7 @@ def reduce( keepdims: bool = False, **kwargs: Any, ) -> "{obj}": - ...""" - - -CLASS_PREAMBLE = """ - -class {obj}{cls}Reductions({obj}Reduce): - __slots__ = ()""" + return NotImplemented""" TEMPLATE_REDUCTION_SIGNATURE = ''' def {method}( @@ -388,8 +379,6 @@ class DataStructure: if __name__ == "__main__": print(MODULE_PREAMBLE) - print(OBJ_PREAMBLE.format(obj="Dataset")) - print(OBJ_PREAMBLE.format(obj="DataArray")) for gen in [ DatasetGenerator, DataArrayGenerator, From 6a9a1240aa95d71ef2081f6e98152981f9db336d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 20 Nov 2021 17:02:07 +0000 Subject: [PATCH 12/31] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/core/_reductions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/_reductions.py b/xarray/core/_reductions.py index e17b9dbd6b8..f280f87434c 100644 --- a/xarray/core/_reductions.py +++ b/xarray/core/_reductions.py @@ -6,8 +6,8 @@ from . import duck_array_ops if TYPE_CHECKING: - from .dataset import Dataset from .dataarray import DataArray + from .dataset import Dataset class DatasetReductions: From dd28a57f66188db47a05fad184519295d688213d Mon Sep 17 00:00:00 2001 From: dcherian Date: Sat, 20 Nov 2021 10:57:22 -0700 Subject: [PATCH 13/31] updates --- xarray/core/_reductions.py | 994 +++++++++++++++++++++++++++++ xarray/util/generate_reductions.py | 6 +- 2 files changed, 997 insertions(+), 3 deletions(-) diff --git a/xarray/core/_reductions.py b/xarray/core/_reductions.py index f280f87434c..6b32c60fbaf 100644 --- a/xarray/core/_reductions.py +++ b/xarray/core/_reductions.py @@ -73,8 +73,19 @@ def count( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.count() + + Dimensions: () + Data variables: + da int64 5 """ return self.reduce( duck_array_ops.count, @@ -133,8 +144,19 @@ def all( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.all() + + Dimensions: () + Data variables: + da bool False """ return self.reduce( duck_array_ops.array_all, @@ -193,8 +215,19 @@ def any( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.any() + + Dimensions: () + Data variables: + da bool True """ return self.reduce( duck_array_ops.array_any, @@ -259,12 +292,27 @@ def max( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.max() + + Dimensions: () + Data variables: + da float64 3.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.max(skipna=False) + + Dimensions: () + Data variables: + da float64 nan """ return self.reduce( duck_array_ops.max, @@ -330,12 +378,27 @@ def min( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.min() + + Dimensions: () + Data variables: + da float64 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.min(skipna=False) + + Dimensions: () + Data variables: + da float64 nan """ return self.reduce( duck_array_ops.min, @@ -405,12 +468,27 @@ def mean( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.mean() + + Dimensions: () + Data variables: + da float64 1.8 Use ``skipna`` to control whether NaNs are ignored. >>> ds.mean(skipna=False) + + Dimensions: () + Data variables: + da float64 nan """ return self.reduce( duck_array_ops.mean, @@ -487,16 +565,35 @@ def prod( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.prod() + + Dimensions: () + Data variables: + da float64 12.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.prod(skipna=False) + + Dimensions: () + Data variables: + da float64 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.prod(skipna=True, min_count=2) + + Dimensions: () + Data variables: + da float64 12.0 """ return self.reduce( duck_array_ops.prod, @@ -574,16 +671,35 @@ def sum( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.sum() + + Dimensions: () + Data variables: + da float64 9.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.sum(skipna=False) + + Dimensions: () + Data variables: + da float64 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.sum(skipna=True, min_count=2) + + Dimensions: () + Data variables: + da float64 9.0 """ return self.reduce( duck_array_ops.sum, @@ -658,16 +774,35 @@ def std( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.std() + + Dimensions: () + Data variables: + da float64 0.7483 Use ``skipna`` to control whether NaNs are ignored. >>> ds.std(skipna=False) + + Dimensions: () + Data variables: + da float64 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.std(skipna=True, ddof=1) + + Dimensions: () + Data variables: + da float64 0.8367 """ return self.reduce( duck_array_ops.std, @@ -742,16 +877,35 @@ def var( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.var() + + Dimensions: () + Data variables: + da float64 0.56 Use ``skipna`` to control whether NaNs are ignored. >>> ds.var(skipna=False) + + Dimensions: () + Data variables: + da float64 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.var(skipna=True, ddof=1) + + Dimensions: () + Data variables: + da float64 0.7 """ return self.reduce( duck_array_ops.var, @@ -822,12 +976,27 @@ def median( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.median() + + Dimensions: () + Data variables: + da float64 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.median(skipna=False) + + Dimensions: () + Data variables: + da float64 nan """ return self.reduce( duck_array_ops.median, @@ -901,8 +1070,15 @@ def count( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.count() + + array(5) """ return self.reduce( duck_array_ops.count, @@ -959,8 +1135,15 @@ def all( ... ), ... ) >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.all() + + array(False) """ return self.reduce( duck_array_ops.array_all, @@ -1017,8 +1200,15 @@ def any( ... ), ... ) >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.any() + + array(True) """ return self.reduce( duck_array_ops.array_any, @@ -1081,12 +1271,21 @@ def max( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.max() + + array(3.) Use ``skipna`` to control whether NaNs are ignored. >>> da.max(skipna=False) + + array(nan) """ return self.reduce( duck_array_ops.max, @@ -1150,12 +1349,21 @@ def min( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.min() + + array(1.) Use ``skipna`` to control whether NaNs are ignored. >>> da.min(skipna=False) + + array(nan) """ return self.reduce( duck_array_ops.min, @@ -1223,12 +1431,21 @@ def mean( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.mean() + + array(1.8) Use ``skipna`` to control whether NaNs are ignored. >>> da.mean(skipna=False) + + array(nan) """ return self.reduce( duck_array_ops.mean, @@ -1303,16 +1520,27 @@ def prod( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.prod() + + array(12.) Use ``skipna`` to control whether NaNs are ignored. >>> da.prod(skipna=False) + + array(nan) Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.prod(skipna=True, min_count=2) + + array(12.) """ return self.reduce( duck_array_ops.prod, @@ -1388,16 +1616,27 @@ def sum( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.sum() + + array(9.) Use ``skipna`` to control whether NaNs are ignored. >>> da.sum(skipna=False) + + array(nan) Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.sum(skipna=True, min_count=2) + + array(9.) """ return self.reduce( duck_array_ops.sum, @@ -1470,16 +1709,27 @@ def std( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.std() + + array(0.74833148) Use ``skipna`` to control whether NaNs are ignored. >>> da.std(skipna=False) + + array(nan) Specify ``ddof=1`` for an unbiased estimate. >>> da.std(skipna=True, ddof=1) + + array(0.83666003) """ return self.reduce( duck_array_ops.std, @@ -1552,16 +1802,27 @@ def var( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.var() + + array(0.56) Use ``skipna`` to control whether NaNs are ignored. >>> da.var(skipna=False) + + array(nan) Specify ``ddof=1`` for an unbiased estimate. >>> da.var(skipna=True, ddof=1) + + array(0.7) """ return self.reduce( duck_array_ops.var, @@ -1630,12 +1891,21 @@ def median( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.median() + + array(2.) Use ``skipna`` to control whether NaNs are ignored. >>> da.median(skipna=False) + + array(nan) """ return self.reduce( duck_array_ops.median, @@ -1709,8 +1979,21 @@ def count( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").count() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) int64 1 2 2 """ return self.reduce( duck_array_ops.count, @@ -1769,8 +2052,21 @@ def all( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").all() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) bool False True True """ return self.reduce( duck_array_ops.array_all, @@ -1829,8 +2125,21 @@ def any( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").any() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) bool True True True """ return self.reduce( duck_array_ops.array_any, @@ -1895,12 +2204,31 @@ def max( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").max() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 1.0 2.0 3.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").max(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 2.0 3.0 """ return self.reduce( duck_array_ops.max, @@ -1966,12 +2294,31 @@ def min( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").min() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 1.0 2.0 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").min(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 2.0 1.0 """ return self.reduce( duck_array_ops.min, @@ -2041,12 +2388,31 @@ def mean( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").mean() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").mean(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 2.0 2.0 """ return self.reduce( duck_array_ops.mean, @@ -2123,16 +2489,41 @@ def prod( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").prod() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 1.0 4.0 3.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").prod(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 4.0 3.0 Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.groupby("labels").prod(skipna=True, min_count=2) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 4.0 3.0 """ return self.reduce( duck_array_ops.prod, @@ -2210,16 +2601,41 @@ def sum( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").sum() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 1.0 4.0 4.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").sum(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 4.0 4.0 Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.groupby("labels").sum(skipna=True, min_count=2) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 4.0 4.0 """ return self.reduce( duck_array_ops.sum, @@ -2294,16 +2710,41 @@ def std( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").std() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 0.0 0.0 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").std(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 0.0 1.0 Specify ``ddof=1`` for an unbiased estimate. >>> ds.groupby("labels").std(skipna=True, ddof=1) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 0.0 1.414 """ return self.reduce( duck_array_ops.std, @@ -2378,16 +2819,41 @@ def var( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").var() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 0.0 0.0 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").var(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 0.0 1.0 Specify ``ddof=1`` for an unbiased estimate. >>> ds.groupby("labels").var(skipna=True, ddof=1) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 0.0 2.0 """ return self.reduce( duck_array_ops.var, @@ -2458,12 +2924,31 @@ def median( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").median() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").median(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 2.0 2.0 """ return self.reduce( duck_array_ops.median, @@ -2538,8 +3023,21 @@ def count( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").count() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) int64 1 3 1 """ return self.reduce( duck_array_ops.count, @@ -2598,8 +3096,21 @@ def all( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").all() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) bool True True False """ return self.reduce( duck_array_ops.array_all, @@ -2658,8 +3169,21 @@ def any( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").any() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) bool True True True """ return self.reduce( duck_array_ops.array_any, @@ -2724,12 +3248,31 @@ def max( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").max() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 3.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").max(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 3.0 nan """ return self.reduce( duck_array_ops.max, @@ -2795,12 +3338,31 @@ def min( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").min() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 1.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").min(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 1.0 nan """ return self.reduce( duck_array_ops.min, @@ -2870,12 +3432,31 @@ def mean( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").mean() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").mean(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 2.0 nan """ return self.reduce( duck_array_ops.mean, @@ -2952,16 +3533,41 @@ def prod( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").prod() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 6.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").prod(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 6.0 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.resample(time="3M").prod(skipna=True, min_count=2) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 nan 6.0 nan """ return self.reduce( duck_array_ops.prod, @@ -3039,16 +3645,41 @@ def sum( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").sum() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 6.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").sum(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 6.0 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.resample(time="3M").sum(skipna=True, min_count=2) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 nan 6.0 nan """ return self.reduce( duck_array_ops.sum, @@ -3123,16 +3754,41 @@ def std( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").std() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 0.0 0.8165 0.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").std(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 0.0 0.8165 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.resample(time="3M").std(skipna=True, ddof=1) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 nan 1.0 nan """ return self.reduce( duck_array_ops.std, @@ -3207,16 +3863,41 @@ def var( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").var() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 0.0 0.6667 0.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").var(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 0.0 0.6667 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.resample(time="3M").var(skipna=True, ddof=1) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 nan 1.0 nan """ return self.reduce( duck_array_ops.var, @@ -3287,12 +3968,31 @@ def median( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").median() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").median(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 2.0 nan """ return self.reduce( duck_array_ops.median, @@ -3366,8 +4066,17 @@ def count( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").count() + + array([1, 2, 2]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.count, @@ -3424,8 +4133,17 @@ def all( ... ), ... ) >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").all() + + array([False, True, True]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.array_all, @@ -3482,8 +4200,17 @@ def any( ... ), ... ) >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").any() + + array([ True, True, True]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.array_any, @@ -3546,12 +4273,25 @@ def max( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").max() + + array([1., 2., 3.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").max(skipna=False) + + array([nan, 2., 3.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.max, @@ -3615,12 +4355,25 @@ def min( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").min() + + array([1., 2., 1.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").min(skipna=False) + + array([nan, 2., 1.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.min, @@ -3688,12 +4441,25 @@ def mean( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").mean() + + array([1., 2., 2.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").mean(skipna=False) + + array([nan, 2., 2.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.mean, @@ -3768,16 +4534,33 @@ def prod( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").prod() + + array([1., 4., 3.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").prod(skipna=False) + + array([nan, 4., 3.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.groupby("labels").prod(skipna=True, min_count=2) + + array([nan, 4., 3.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.prod, @@ -3853,16 +4636,33 @@ def sum( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").sum() + + array([1., 4., 4.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").sum(skipna=False) + + array([nan, 4., 4.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.groupby("labels").sum(skipna=True, min_count=2) + + array([nan, 4., 4.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.sum, @@ -3935,16 +4735,33 @@ def std( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").std() + + array([0., 0., 1.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").std(skipna=False) + + array([nan, 0., 1.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Specify ``ddof=1`` for an unbiased estimate. >>> da.groupby("labels").std(skipna=True, ddof=1) + + array([ nan, 0. , 1.41421356]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.std, @@ -4017,16 +4834,33 @@ def var( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").var() + + array([0., 0., 1.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").var(skipna=False) + + array([nan, 0., 1.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Specify ``ddof=1`` for an unbiased estimate. >>> da.groupby("labels").var(skipna=True, ddof=1) + + array([nan, 0., 2.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.var, @@ -4095,12 +4929,25 @@ def median( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").median() + + array([1., 2., 2.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").median(skipna=False) + + array([nan, 2., 2.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.median, @@ -4173,8 +5020,17 @@ def count( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").count() + + array([1, 3, 1]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.count, @@ -4231,8 +5087,17 @@ def all( ... ), ... ) >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").all() + + array([ True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.array_all, @@ -4289,8 +5154,17 @@ def any( ... ), ... ) >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").any() + + array([ True, True, True]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.array_any, @@ -4353,12 +5227,25 @@ def max( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").max() + + array([1., 3., 2.]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").max(skipna=False) + + array([ 1., 3., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.max, @@ -4422,12 +5309,25 @@ def min( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").min() + + array([1., 1., 2.]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").min(skipna=False) + + array([ 1., 1., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.min, @@ -4495,12 +5395,25 @@ def mean( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").mean() + + array([1., 2., 2.]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").mean(skipna=False) + + array([ 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.mean, @@ -4575,16 +5488,33 @@ def prod( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").prod() + + array([1., 6., 2.]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").prod(skipna=False) + + array([ 1., 6., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.resample(time="3M").prod(skipna=True, min_count=2) + + array([nan, 6., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.prod, @@ -4660,16 +5590,33 @@ def sum( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").sum() + + array([1., 6., 2.]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").sum(skipna=False) + + array([ 1., 6., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.resample(time="3M").sum(skipna=True, min_count=2) + + array([nan, 6., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.sum, @@ -4742,16 +5689,33 @@ def std( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").std() + + array([0. , 0.81649658, 0. ]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").std(skipna=False) + + array([0. , 0.81649658, nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``ddof=1`` for an unbiased estimate. >>> da.resample(time="3M").std(skipna=True, ddof=1) + + array([nan, 1., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.std, @@ -4824,16 +5788,33 @@ def var( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").var() + + array([0. , 0.66666667, 0. ]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").var(skipna=False) + + array([0. , 0.66666667, nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``ddof=1`` for an unbiased estimate. >>> da.resample(time="3M").var(skipna=True, ddof=1) + + array([nan, 1., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.var, @@ -4902,12 +5883,25 @@ def median( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").median() + + array([1., 2., 2.]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").median(skipna=False) + + array([ 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.median, diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index ea479ccc9c3..4981efcc7e7 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -5,7 +5,7 @@ Usage: python xarray/util/generate_reductions.py > xarray/core/_reductions.py pytest --doctest-modules xarray/core/_reductions.py --accept || true - pytest --doctest-modules xarray/core/_reductions.py --accept + pytest --doctest-modules xarray/core/_reductions.py This requires [pytest-accept](https://github.com/max-sixty/pytest-accept). The second run of pytest is deliberate, since the first will return an error @@ -25,8 +25,8 @@ from . import duck_array_ops if TYPE_CHECKING: - from .dataset import Dataset - from .dataarray import DataArray''' + from .dataarray import DataArray + from .dataset import Dataset''' CLASS_PREAMBLE = """ From 2bbddafaacf46f65c950738a9db3cbaddb198763 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 20 Nov 2021 20:12:31 +0100 Subject: [PATCH 14/31] make reduce args consistent --- xarray/core/_reductions.py | 1000 +--------------------------- xarray/core/dataarray.py | 1 + xarray/core/dataset.py | 3 +- xarray/core/groupby.py | 46 +- xarray/core/resample.py | 20 +- xarray/util/generate_reductions.py | 1 + 6 files changed, 64 insertions(+), 1007 deletions(-) diff --git a/xarray/core/_reductions.py b/xarray/core/_reductions.py index 6b32c60fbaf..240cc655297 100644 --- a/xarray/core/_reductions.py +++ b/xarray/core/_reductions.py @@ -17,6 +17,7 @@ def reduce( self, func: Callable[..., Any], dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, axis: Union[None, int, Sequence[int]] = None, keep_attrs: bool = None, keepdims: bool = False, @@ -73,19 +74,8 @@ def count( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.count() - - Dimensions: () - Data variables: - da int64 5 """ return self.reduce( duck_array_ops.count, @@ -144,19 +134,8 @@ def all( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.all() - - Dimensions: () - Data variables: - da bool False """ return self.reduce( duck_array_ops.array_all, @@ -215,19 +194,8 @@ def any( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.any() - - Dimensions: () - Data variables: - da bool True """ return self.reduce( duck_array_ops.array_any, @@ -292,27 +260,12 @@ def max( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.max() - - Dimensions: () - Data variables: - da float64 3.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.max(skipna=False) - - Dimensions: () - Data variables: - da float64 nan """ return self.reduce( duck_array_ops.max, @@ -378,27 +331,12 @@ def min( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.min() - - Dimensions: () - Data variables: - da float64 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.min(skipna=False) - - Dimensions: () - Data variables: - da float64 nan """ return self.reduce( duck_array_ops.min, @@ -468,27 +406,12 @@ def mean( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.mean() - - Dimensions: () - Data variables: - da float64 1.8 Use ``skipna`` to control whether NaNs are ignored. >>> ds.mean(skipna=False) - - Dimensions: () - Data variables: - da float64 nan """ return self.reduce( duck_array_ops.mean, @@ -565,35 +488,16 @@ def prod( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.prod() - - Dimensions: () - Data variables: - da float64 12.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.prod(skipna=False) - - Dimensions: () - Data variables: - da float64 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.prod(skipna=True, min_count=2) - - Dimensions: () - Data variables: - da float64 12.0 """ return self.reduce( duck_array_ops.prod, @@ -671,35 +575,16 @@ def sum( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.sum() - - Dimensions: () - Data variables: - da float64 9.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.sum(skipna=False) - - Dimensions: () - Data variables: - da float64 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.sum(skipna=True, min_count=2) - - Dimensions: () - Data variables: - da float64 9.0 """ return self.reduce( duck_array_ops.sum, @@ -774,35 +659,16 @@ def std( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.std() - - Dimensions: () - Data variables: - da float64 0.7483 Use ``skipna`` to control whether NaNs are ignored. >>> ds.std(skipna=False) - - Dimensions: () - Data variables: - da float64 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.std(skipna=True, ddof=1) - - Dimensions: () - Data variables: - da float64 0.8367 """ return self.reduce( duck_array_ops.std, @@ -877,35 +743,16 @@ def var( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.var() - - Dimensions: () - Data variables: - da float64 0.56 Use ``skipna`` to control whether NaNs are ignored. >>> ds.var(skipna=False) - - Dimensions: () - Data variables: - da float64 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.var(skipna=True, ddof=1) - - Dimensions: () - Data variables: - da float64 0.7 """ return self.reduce( duck_array_ops.var, @@ -976,27 +823,12 @@ def median( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.median() - - Dimensions: () - Data variables: - da float64 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.median(skipna=False) - - Dimensions: () - Data variables: - da float64 nan """ return self.reduce( duck_array_ops.median, @@ -1015,6 +847,7 @@ def reduce( self, func: Callable[..., Any], dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, axis: Union[None, int, Sequence[int]] = None, keep_attrs: bool = None, keepdims: bool = False, @@ -1070,15 +903,8 @@ def count( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.count() - - array(5) """ return self.reduce( duck_array_ops.count, @@ -1135,15 +961,8 @@ def all( ... ), ... ) >>> da - - array([ True, True, True, True, True, False]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.all() - - array(False) """ return self.reduce( duck_array_ops.array_all, @@ -1200,15 +1019,8 @@ def any( ... ), ... ) >>> da - - array([ True, True, True, True, True, False]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.any() - - array(True) """ return self.reduce( duck_array_ops.array_any, @@ -1271,21 +1083,12 @@ def max( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.max() - - array(3.) Use ``skipna`` to control whether NaNs are ignored. >>> da.max(skipna=False) - - array(nan) """ return self.reduce( duck_array_ops.max, @@ -1349,21 +1152,12 @@ def min( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.min() - - array(1.) Use ``skipna`` to control whether NaNs are ignored. >>> da.min(skipna=False) - - array(nan) """ return self.reduce( duck_array_ops.min, @@ -1431,21 +1225,12 @@ def mean( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.mean() - - array(1.8) Use ``skipna`` to control whether NaNs are ignored. >>> da.mean(skipna=False) - - array(nan) """ return self.reduce( duck_array_ops.mean, @@ -1520,27 +1305,16 @@ def prod( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.prod() - - array(12.) Use ``skipna`` to control whether NaNs are ignored. >>> da.prod(skipna=False) - - array(nan) Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.prod(skipna=True, min_count=2) - - array(12.) """ return self.reduce( duck_array_ops.prod, @@ -1616,27 +1390,16 @@ def sum( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.sum() - - array(9.) Use ``skipna`` to control whether NaNs are ignored. >>> da.sum(skipna=False) - - array(nan) Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.sum(skipna=True, min_count=2) - - array(9.) """ return self.reduce( duck_array_ops.sum, @@ -1709,27 +1472,16 @@ def std( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.std() - - array(0.74833148) Use ``skipna`` to control whether NaNs are ignored. >>> da.std(skipna=False) - - array(nan) Specify ``ddof=1`` for an unbiased estimate. >>> da.std(skipna=True, ddof=1) - - array(0.83666003) """ return self.reduce( duck_array_ops.std, @@ -1802,27 +1554,16 @@ def var( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.var() - - array(0.56) Use ``skipna`` to control whether NaNs are ignored. >>> da.var(skipna=False) - - array(nan) Specify ``ddof=1`` for an unbiased estimate. >>> da.var(skipna=True, ddof=1) - - array(0.7) """ return self.reduce( duck_array_ops.var, @@ -1891,21 +1632,12 @@ def median( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.median() - - array(2.) Use ``skipna`` to control whether NaNs are ignored. >>> da.median(skipna=False) - - array(nan) """ return self.reduce( duck_array_ops.median, @@ -1923,6 +1655,7 @@ def reduce( self, func: Callable[..., Any], dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, axis: Union[None, int, Sequence[int]] = None, keep_attrs: bool = None, keepdims: bool = False, @@ -1979,21 +1712,8 @@ def count( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").count() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) int64 1 2 2 """ return self.reduce( duck_array_ops.count, @@ -2052,21 +1772,8 @@ def all( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").all() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) bool False True True """ return self.reduce( duck_array_ops.array_all, @@ -2125,21 +1832,8 @@ def any( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").any() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) bool True True True """ return self.reduce( duck_array_ops.array_any, @@ -2204,31 +1898,12 @@ def max( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").max() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 1.0 2.0 3.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").max(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 2.0 3.0 """ return self.reduce( duck_array_ops.max, @@ -2294,31 +1969,12 @@ def min( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").min() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 1.0 2.0 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").min(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 2.0 1.0 """ return self.reduce( duck_array_ops.min, @@ -2388,31 +2044,12 @@ def mean( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").mean() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").mean(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 2.0 2.0 """ return self.reduce( duck_array_ops.mean, @@ -2489,41 +2126,16 @@ def prod( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").prod() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 1.0 4.0 3.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").prod(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 4.0 3.0 Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.groupby("labels").prod(skipna=True, min_count=2) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 4.0 3.0 """ return self.reduce( duck_array_ops.prod, @@ -2601,41 +2213,16 @@ def sum( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").sum() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 1.0 4.0 4.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").sum(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 4.0 4.0 Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.groupby("labels").sum(skipna=True, min_count=2) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 4.0 4.0 """ return self.reduce( duck_array_ops.sum, @@ -2710,41 +2297,16 @@ def std( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").std() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 0.0 0.0 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").std(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 0.0 1.0 Specify ``ddof=1`` for an unbiased estimate. >>> ds.groupby("labels").std(skipna=True, ddof=1) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 0.0 1.414 """ return self.reduce( duck_array_ops.std, @@ -2819,41 +2381,16 @@ def var( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").var() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 0.0 0.0 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").var(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 0.0 1.0 Specify ``ddof=1`` for an unbiased estimate. >>> ds.groupby("labels").var(skipna=True, ddof=1) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 0.0 2.0 """ return self.reduce( duck_array_ops.var, @@ -2924,31 +2461,12 @@ def median( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.groupby("labels").median() - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").median(skipna=False) - - Dimensions: (labels: 3) - Coordinates: - * labels (labels) object 'a' 'b' 'c' - Data variables: - da (labels) float64 nan 2.0 2.0 """ return self.reduce( duck_array_ops.median, @@ -2967,6 +2485,7 @@ def reduce( self, func: Callable[..., Any], dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, axis: Union[None, int, Sequence[int]] = None, keep_attrs: bool = None, keepdims: bool = False, @@ -3023,21 +2542,8 @@ def count( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").count() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) int64 1 3 1 """ return self.reduce( duck_array_ops.count, @@ -3096,21 +2602,8 @@ def all( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").all() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) bool True True False """ return self.reduce( duck_array_ops.array_all, @@ -3169,21 +2662,8 @@ def any( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").any() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) bool True True True """ return self.reduce( duck_array_ops.array_any, @@ -3248,31 +2728,12 @@ def max( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").max() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 3.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").max(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 3.0 nan """ return self.reduce( duck_array_ops.max, @@ -3338,31 +2799,12 @@ def min( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").min() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 1.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").min(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 1.0 nan """ return self.reduce( duck_array_ops.min, @@ -3432,31 +2874,12 @@ def mean( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").mean() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").mean(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 2.0 nan """ return self.reduce( duck_array_ops.mean, @@ -3533,41 +2956,16 @@ def prod( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").prod() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 6.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").prod(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 6.0 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.resample(time="3M").prod(skipna=True, min_count=2) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 nan 6.0 nan """ return self.reduce( duck_array_ops.prod, @@ -3645,41 +3043,16 @@ def sum( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").sum() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 6.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").sum(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 6.0 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.resample(time="3M").sum(skipna=True, min_count=2) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 nan 6.0 nan """ return self.reduce( duck_array_ops.sum, @@ -3754,41 +3127,16 @@ def std( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").std() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 0.0 0.8165 0.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").std(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 0.0 0.8165 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.resample(time="3M").std(skipna=True, ddof=1) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 nan 1.0 nan """ return self.reduce( duck_array_ops.std, @@ -3863,41 +3211,16 @@ def var( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").var() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 0.0 0.6667 0.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").var(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 0.0 0.6667 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.resample(time="3M").var(skipna=True, ddof=1) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 nan 1.0 nan """ return self.reduce( duck_array_ops.var, @@ -3968,31 +3291,12 @@ def median( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds - - Dimensions: (time: 6) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> ds.resample(time="3M").median() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").median(skipna=False) - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) float64 1.0 2.0 nan """ return self.reduce( duck_array_ops.median, @@ -4011,6 +3315,7 @@ def reduce( self, func: Callable[..., Any], dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, axis: Union[None, int, Sequence[int]] = None, keep_attrs: bool = None, keepdims: bool = False, @@ -4066,17 +3371,8 @@ def count( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").count() - - array([1, 2, 2]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.count, @@ -4133,17 +3429,8 @@ def all( ... ), ... ) >>> da - - array([ True, True, True, True, True, False]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").all() - - array([False, True, True]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.array_all, @@ -4200,17 +3487,8 @@ def any( ... ), ... ) >>> da - - array([ True, True, True, True, True, False]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").any() - - array([ True, True, True]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.array_any, @@ -4273,25 +3551,12 @@ def max( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").max() - - array([1., 2., 3.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").max(skipna=False) - - array([nan, 2., 3.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.max, @@ -4355,25 +3620,12 @@ def min( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").min() - - array([1., 2., 1.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").min(skipna=False) - - array([nan, 2., 1.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.min, @@ -4441,25 +3693,12 @@ def mean( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").mean() - - array([1., 2., 2.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").mean(skipna=False) - - array([nan, 2., 2.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.mean, @@ -4534,33 +3773,16 @@ def prod( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").prod() - - array([1., 4., 3.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").prod(skipna=False) - - array([nan, 4., 3.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.groupby("labels").prod(skipna=True, min_count=2) - - array([nan, 4., 3.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.prod, @@ -4636,33 +3858,16 @@ def sum( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").sum() - - array([1., 4., 4.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").sum(skipna=False) - - array([nan, 4., 4.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.groupby("labels").sum(skipna=True, min_count=2) - - array([nan, 4., 4.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.sum, @@ -4735,33 +3940,16 @@ def std( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").std() - - array([0., 0., 1.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").std(skipna=False) - - array([nan, 0., 1.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Specify ``ddof=1`` for an unbiased estimate. >>> da.groupby("labels").std(skipna=True, ddof=1) - - array([ nan, 0. , 1.41421356]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.std, @@ -4834,33 +4022,16 @@ def var( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").var() - - array([0., 0., 1.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").var(skipna=False) - - array([nan, 0., 1.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Specify ``ddof=1`` for an unbiased estimate. >>> da.groupby("labels").var(skipna=True, ddof=1) - - array([nan, 0., 2.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.var, @@ -4929,25 +4100,12 @@ def median( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.groupby("labels").median() - - array([1., 2., 2.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").median(skipna=False) - - array([nan, 2., 2.]) - Coordinates: - * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.median, @@ -4965,6 +4123,7 @@ def reduce( self, func: Callable[..., Any], dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, axis: Union[None, int, Sequence[int]] = None, keep_attrs: bool = None, keepdims: bool = False, @@ -5020,17 +4179,8 @@ def count( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").count() - - array([1, 3, 1]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.count, @@ -5087,17 +4237,8 @@ def all( ... ), ... ) >>> da - - array([ True, True, True, True, True, False]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").all() - - array([ True, True, False]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.array_all, @@ -5154,17 +4295,8 @@ def any( ... ), ... ) >>> da - - array([ True, True, True, True, True, False]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").any() - - array([ True, True, True]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.array_any, @@ -5227,25 +4359,12 @@ def max( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").max() - - array([1., 3., 2.]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").max(skipna=False) - - array([ 1., 3., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.max, @@ -5309,25 +4428,12 @@ def min( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").min() - - array([1., 1., 2.]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").min(skipna=False) - - array([ 1., 1., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.min, @@ -5395,25 +4501,12 @@ def mean( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").mean() - - array([1., 2., 2.]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").mean(skipna=False) - - array([ 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.mean, @@ -5488,33 +4581,16 @@ def prod( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").prod() - - array([1., 6., 2.]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").prod(skipna=False) - - array([ 1., 6., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.resample(time="3M").prod(skipna=True, min_count=2) - - array([nan, 6., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.prod, @@ -5590,33 +4666,16 @@ def sum( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").sum() - - array([1., 6., 2.]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").sum(skipna=False) - - array([ 1., 6., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.resample(time="3M").sum(skipna=True, min_count=2) - - array([nan, 6., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.sum, @@ -5689,33 +4748,16 @@ def std( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").std() - - array([0. , 0.81649658, 0. ]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").std(skipna=False) - - array([0. , 0.81649658, nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``ddof=1`` for an unbiased estimate. >>> da.resample(time="3M").std(skipna=True, ddof=1) - - array([nan, 1., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.std, @@ -5788,33 +4830,16 @@ def var( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").var() - - array([0. , 0.66666667, 0. ]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").var(skipna=False) - - array([0. , 0.66666667, nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``ddof=1`` for an unbiased estimate. >>> da.resample(time="3M").var(skipna=True, ddof=1) - - array([nan, 1., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.var, @@ -5883,25 +4908,12 @@ def median( ... ), ... ) >>> da - - array([ 1., 2., 3., 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 - labels (time) >> da.resample(time="3M").median() - - array([1., 2., 2.]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").median(skipna=False) - - array([ 1., 2., nan]) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.median, diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index ea03ac76f37..1b96f22b744 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -2655,6 +2655,7 @@ def reduce( self, func: Callable[..., Any], dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, axis: Union[None, int, Sequence[int]] = None, keep_attrs: bool = None, keepdims: bool = False, diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index e3339f2562f..cf52fed6974 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -5000,6 +5000,7 @@ def reduce( self, func: Callable, dim: Union[Hashable, Iterable[Hashable]] = None, + *, keep_attrs: bool = None, keepdims: bool = False, numeric_only: bool = False, @@ -5035,7 +5036,7 @@ def reduce( Dataset with this object's DataArrays replaced with new DataArrays of summarized data and the indicated dimension(s) removed. """ - if "axis" in kwargs: + if kwargs.get("axis", None) is not None: raise ValueError( "passing 'axis' to Dataset reduce methods is ambiguous." " Please use 'dim' instead." diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index 185b4ae5bec..bf5cded33ee 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -1,5 +1,6 @@ import datetime import warnings +from typing import Any, Callable, Hashable, Sequence, Union import numpy as np import pandas as pd @@ -834,7 +835,15 @@ def _combine(self, applied, shortcut=False): return combined def reduce( - self, func, dim=None, axis=None, keep_attrs=None, shortcut=True, **kwargs + self, + func: Callable[..., Any], + dim: Union[None, Hashable, Sequence[Hashable]] = None, + axis: Union[None, int, Sequence[int]] = None, + keep_attrs: bool = None, + keepdims: bool = False, + *, + shortcut: bool = True, + **kwargs: Any, ): """Reduce the items in this group by applying `func` along some dimension(s). @@ -867,11 +876,15 @@ def reduce( if dim is None: dim = self._group_dim - if keep_attrs is None: - keep_attrs = _get_keep_attrs(default=False) - def reduce_array(ar): - return ar.reduce(func, dim, axis, keep_attrs=keep_attrs, **kwargs) + return ar.reduce( + func=func, + dim=dim, + axis=axis, + keep_attrs=keep_attrs, + keepdims=keepdims, + **kwargs, + ) check_reduce_dims(dim, self.dims) @@ -949,7 +962,16 @@ def _combine(self, applied): combined = self._maybe_unstack(combined) return combined - def reduce(self, func, dim=None, keep_attrs=None, **kwargs): + def reduce( + self, + func: Callable[..., Any], + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + axis: Union[None, int, Sequence[int]] = None, + keep_attrs: bool = None, + keepdims: bool = False, + **kwargs: Any, + ): """Reduce the items in this group by applying `func` along some dimension(s). @@ -981,11 +1003,15 @@ def reduce(self, func, dim=None, keep_attrs=None, **kwargs): if dim is None: dim = self._group_dim - if keep_attrs is None: - keep_attrs = _get_keep_attrs(default=False) - def reduce_dataset(ds): - return ds.reduce(func, dim, keep_attrs, **kwargs) + return ds.reduce( + func=func, + dim=dim, + axis=axis, + keep_attrs=keep_attrs, + keepdims=keepdims, + **kwargs, + ) check_reduce_dims(dim, self.dims) diff --git a/xarray/core/resample.py b/xarray/core/resample.py index ad11550372c..f1776cf3078 100644 --- a/xarray/core/resample.py +++ b/xarray/core/resample.py @@ -1,4 +1,5 @@ import warnings +from typing import Any, Callable, Hashable, Sequence, Union from ._reductions import DataArrayResampleReductions, DatasetResampleReductions from .groupby import DataArrayGroupByBase, DatasetGroupByBase @@ -316,7 +317,15 @@ def apply(self, func, args=(), shortcut=None, **kwargs): ) return self.map(func=func, shortcut=shortcut, args=args, **kwargs) - def reduce(self, func, dim=None, keep_attrs=None, **kwargs): + def reduce( + self, + func: Callable[..., Any], + dim: Union[None, Hashable, Sequence[Hashable]] = None, + axis: Union[None, int, Sequence[int]] = None, + keep_attrs: bool = None, + keepdims: bool = False, + **kwargs: Any, + ): """Reduce the items in this group by applying `func` along the pre-defined resampling dimension. @@ -341,4 +350,11 @@ def reduce(self, func, dim=None, keep_attrs=None, **kwargs): Array with summarized data and the indicated dimension(s) removed. """ - return super().reduce(func, dim, keep_attrs, **kwargs) + return super().reduce( + func=func, + dim=dim, + axis=axis, + keep_attrs=keep_attrs, + keepdims=keepdims, + **kwargs, + ) diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index 4981efcc7e7..fbf2d82dd2c 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -38,6 +38,7 @@ def reduce( self, func: Callable[..., Any], dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, axis: Union[None, int, Sequence[int]] = None, keep_attrs: bool = None, keepdims: bool = False, From 3d854e52055de0f53f4ba16b0713ac581611ef94 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 20 Nov 2021 20:21:26 +0100 Subject: [PATCH 15/31] more reduce edits --- xarray/core/common.py | 2 +- xarray/core/groupby.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/core/common.py b/xarray/core/common.py index b5dc3bf0e20..e2a5e0926a8 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -65,7 +65,7 @@ def wrapped_func(self, dim=None, axis=None, skipna=None, **kwargs): else: def wrapped_func(self, dim=None, axis=None, **kwargs): # type: ignore[misc] - return self.reduce(func, dim, axis, **kwargs) + return self.reduce(func=func, dim=dim, axis=axis, **kwargs) return wrapped_func diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index bf5cded33ee..df2adaad260 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -838,10 +838,10 @@ def reduce( self, func: Callable[..., Any], dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, axis: Union[None, int, Sequence[int]] = None, keep_attrs: bool = None, keepdims: bool = False, - *, shortcut: bool = True, **kwargs: Any, ): From be33560a14ac4b9379e5d0ff4f340cfbd6d552f1 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 20 Nov 2021 20:28:39 +0100 Subject: [PATCH 16/31] one more reduce --- xarray/core/common.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xarray/core/common.py b/xarray/core/common.py index e2a5e0926a8..7e6bbc8b05f 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -60,7 +60,9 @@ def _reduce_method(cls, func: Callable, include_skipna: bool, numeric_only: bool if include_skipna: def wrapped_func(self, dim=None, axis=None, skipna=None, **kwargs): - return self.reduce(func, dim, axis, skipna=skipna, **kwargs) + return self.reduce( + func=func, dim=dim, axis=axis, skipna=skipna, **kwargs + ) else: From 0f94bec2953aa3e7eadd0c4efc25cd6111b7e663 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 20 Nov 2021 20:48:27 +0100 Subject: [PATCH 17/31] another reduce --- xarray/core/resample.py | 1 + 1 file changed, 1 insertion(+) diff --git a/xarray/core/resample.py b/xarray/core/resample.py index f1776cf3078..ed665ad4048 100644 --- a/xarray/core/resample.py +++ b/xarray/core/resample.py @@ -321,6 +321,7 @@ def reduce( self, func: Callable[..., Any], dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, axis: Union[None, int, Sequence[int]] = None, keep_attrs: bool = None, keepdims: bool = False, From 19d82cddf0a4ac30811803de9f7e70a881d52ea0 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 20 Nov 2021 22:00:29 +0100 Subject: [PATCH 18/31] more reduce --- xarray/core/common.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/xarray/core/common.py b/xarray/core/common.py index 7e6bbc8b05f..2300f3dd8f5 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -100,13 +100,19 @@ def _reduce_method(cls, func: Callable, include_skipna: bool, numeric_only: bool def wrapped_func(self, dim=None, skipna=None, **kwargs): return self.reduce( - func, dim, skipna=skipna, numeric_only=numeric_only, **kwargs + func=func, + dim=dim, + skipna=skipna, + numeric_only=numeric_only, + **kwargs, ) else: def wrapped_func(self, dim=None, **kwargs): # type: ignore[misc] - return self.reduce(func, dim, numeric_only=numeric_only, **kwargs) + return self.reduce( + func=func, dim=dim, numeric_only=numeric_only, **kwargs + ) return wrapped_func From cd8a898d5b003ea28ec8f3feacb56d76b6dc1096 Mon Sep 17 00:00:00 2001 From: dcherian Date: Sat, 20 Nov 2021 14:37:17 -0700 Subject: [PATCH 19/31] add doctests --- xarray/core/_reductions.py | 994 +++++++++++++++++++++++++++++++++++++ 1 file changed, 994 insertions(+) diff --git a/xarray/core/_reductions.py b/xarray/core/_reductions.py index 240cc655297..5389aa4e09d 100644 --- a/xarray/core/_reductions.py +++ b/xarray/core/_reductions.py @@ -74,8 +74,19 @@ def count( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.count() + + Dimensions: () + Data variables: + da int64 5 """ return self.reduce( duck_array_ops.count, @@ -134,8 +145,19 @@ def all( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.all() + + Dimensions: () + Data variables: + da bool False """ return self.reduce( duck_array_ops.array_all, @@ -194,8 +216,19 @@ def any( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.any() + + Dimensions: () + Data variables: + da bool True """ return self.reduce( duck_array_ops.array_any, @@ -260,12 +293,27 @@ def max( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.max() + + Dimensions: () + Data variables: + da float64 3.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.max(skipna=False) + + Dimensions: () + Data variables: + da float64 nan """ return self.reduce( duck_array_ops.max, @@ -331,12 +379,27 @@ def min( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.min() + + Dimensions: () + Data variables: + da float64 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.min(skipna=False) + + Dimensions: () + Data variables: + da float64 nan """ return self.reduce( duck_array_ops.min, @@ -406,12 +469,27 @@ def mean( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.mean() + + Dimensions: () + Data variables: + da float64 1.8 Use ``skipna`` to control whether NaNs are ignored. >>> ds.mean(skipna=False) + + Dimensions: () + Data variables: + da float64 nan """ return self.reduce( duck_array_ops.mean, @@ -488,16 +566,35 @@ def prod( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.prod() + + Dimensions: () + Data variables: + da float64 12.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.prod(skipna=False) + + Dimensions: () + Data variables: + da float64 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.prod(skipna=True, min_count=2) + + Dimensions: () + Data variables: + da float64 12.0 """ return self.reduce( duck_array_ops.prod, @@ -575,16 +672,35 @@ def sum( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.sum() + + Dimensions: () + Data variables: + da float64 9.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.sum(skipna=False) + + Dimensions: () + Data variables: + da float64 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.sum(skipna=True, min_count=2) + + Dimensions: () + Data variables: + da float64 9.0 """ return self.reduce( duck_array_ops.sum, @@ -659,16 +775,35 @@ def std( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.std() + + Dimensions: () + Data variables: + da float64 0.7483 Use ``skipna`` to control whether NaNs are ignored. >>> ds.std(skipna=False) + + Dimensions: () + Data variables: + da float64 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.std(skipna=True, ddof=1) + + Dimensions: () + Data variables: + da float64 0.8367 """ return self.reduce( duck_array_ops.std, @@ -743,16 +878,35 @@ def var( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.var() + + Dimensions: () + Data variables: + da float64 0.56 Use ``skipna`` to control whether NaNs are ignored. >>> ds.var(skipna=False) + + Dimensions: () + Data variables: + da float64 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.var(skipna=True, ddof=1) + + Dimensions: () + Data variables: + da float64 0.7 """ return self.reduce( duck_array_ops.var, @@ -823,12 +977,27 @@ def median( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.median() + + Dimensions: () + Data variables: + da float64 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.median(skipna=False) + + Dimensions: () + Data variables: + da float64 nan """ return self.reduce( duck_array_ops.median, @@ -903,8 +1072,15 @@ def count( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.count() + + array(5) """ return self.reduce( duck_array_ops.count, @@ -961,8 +1137,15 @@ def all( ... ), ... ) >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.all() + + array(False) """ return self.reduce( duck_array_ops.array_all, @@ -1019,8 +1202,15 @@ def any( ... ), ... ) >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.any() + + array(True) """ return self.reduce( duck_array_ops.array_any, @@ -1083,12 +1273,21 @@ def max( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.max() + + array(3.) Use ``skipna`` to control whether NaNs are ignored. >>> da.max(skipna=False) + + array(nan) """ return self.reduce( duck_array_ops.max, @@ -1152,12 +1351,21 @@ def min( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.min() + + array(1.) Use ``skipna`` to control whether NaNs are ignored. >>> da.min(skipna=False) + + array(nan) """ return self.reduce( duck_array_ops.min, @@ -1225,12 +1433,21 @@ def mean( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.mean() + + array(1.8) Use ``skipna`` to control whether NaNs are ignored. >>> da.mean(skipna=False) + + array(nan) """ return self.reduce( duck_array_ops.mean, @@ -1305,16 +1522,27 @@ def prod( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.prod() + + array(12.) Use ``skipna`` to control whether NaNs are ignored. >>> da.prod(skipna=False) + + array(nan) Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.prod(skipna=True, min_count=2) + + array(12.) """ return self.reduce( duck_array_ops.prod, @@ -1390,16 +1618,27 @@ def sum( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.sum() + + array(9.) Use ``skipna`` to control whether NaNs are ignored. >>> da.sum(skipna=False) + + array(nan) Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.sum(skipna=True, min_count=2) + + array(9.) """ return self.reduce( duck_array_ops.sum, @@ -1472,16 +1711,27 @@ def std( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.std() + + array(0.74833148) Use ``skipna`` to control whether NaNs are ignored. >>> da.std(skipna=False) + + array(nan) Specify ``ddof=1`` for an unbiased estimate. >>> da.std(skipna=True, ddof=1) + + array(0.83666003) """ return self.reduce( duck_array_ops.std, @@ -1554,16 +1804,27 @@ def var( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.var() + + array(0.56) Use ``skipna`` to control whether NaNs are ignored. >>> da.var(skipna=False) + + array(nan) Specify ``ddof=1`` for an unbiased estimate. >>> da.var(skipna=True, ddof=1) + + array(0.7) """ return self.reduce( duck_array_ops.var, @@ -1632,12 +1893,21 @@ def median( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.median() + + array(2.) Use ``skipna`` to control whether NaNs are ignored. >>> da.median(skipna=False) + + array(nan) """ return self.reduce( duck_array_ops.median, @@ -1712,8 +1982,21 @@ def count( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").count() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) int64 1 2 2 """ return self.reduce( duck_array_ops.count, @@ -1772,8 +2055,21 @@ def all( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").all() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) bool False True True """ return self.reduce( duck_array_ops.array_all, @@ -1832,8 +2128,21 @@ def any( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").any() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) bool True True True """ return self.reduce( duck_array_ops.array_any, @@ -1898,12 +2207,31 @@ def max( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").max() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 1.0 2.0 3.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").max(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 2.0 3.0 """ return self.reduce( duck_array_ops.max, @@ -1969,12 +2297,31 @@ def min( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").min() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 1.0 2.0 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").min(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 2.0 1.0 """ return self.reduce( duck_array_ops.min, @@ -2044,12 +2391,31 @@ def mean( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").mean() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").mean(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 2.0 2.0 """ return self.reduce( duck_array_ops.mean, @@ -2126,16 +2492,41 @@ def prod( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").prod() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 1.0 4.0 3.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").prod(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 4.0 3.0 Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.groupby("labels").prod(skipna=True, min_count=2) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 4.0 3.0 """ return self.reduce( duck_array_ops.prod, @@ -2213,16 +2604,41 @@ def sum( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").sum() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 1.0 4.0 4.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").sum(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 4.0 4.0 Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.groupby("labels").sum(skipna=True, min_count=2) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 4.0 4.0 """ return self.reduce( duck_array_ops.sum, @@ -2297,16 +2713,41 @@ def std( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").std() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 0.0 0.0 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").std(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 0.0 1.0 Specify ``ddof=1`` for an unbiased estimate. >>> ds.groupby("labels").std(skipna=True, ddof=1) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 0.0 1.414 """ return self.reduce( duck_array_ops.std, @@ -2381,16 +2822,41 @@ def var( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").var() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 0.0 0.0 1.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").var(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 0.0 1.0 Specify ``ddof=1`` for an unbiased estimate. >>> ds.groupby("labels").var(skipna=True, ddof=1) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 0.0 2.0 """ return self.reduce( duck_array_ops.var, @@ -2461,12 +2927,31 @@ def median( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.groupby("labels").median() + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.groupby("labels").median(skipna=False) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 2.0 2.0 """ return self.reduce( duck_array_ops.median, @@ -2542,8 +3027,21 @@ def count( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").count() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) int64 1 3 1 """ return self.reduce( duck_array_ops.count, @@ -2602,8 +3100,21 @@ def all( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").all() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) bool True True False """ return self.reduce( duck_array_ops.array_all, @@ -2662,8 +3173,21 @@ def any( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").any() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) bool True True True """ return self.reduce( duck_array_ops.array_any, @@ -2728,12 +3252,31 @@ def max( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").max() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 3.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").max(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 3.0 nan """ return self.reduce( duck_array_ops.max, @@ -2799,12 +3342,31 @@ def min( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").min() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 1.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").min(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 1.0 nan """ return self.reduce( duck_array_ops.min, @@ -2874,12 +3436,31 @@ def mean( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").mean() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").mean(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 2.0 nan """ return self.reduce( duck_array_ops.mean, @@ -2956,16 +3537,41 @@ def prod( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").prod() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 6.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").prod(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 6.0 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.resample(time="3M").prod(skipna=True, min_count=2) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 nan 6.0 nan """ return self.reduce( duck_array_ops.prod, @@ -3043,16 +3649,41 @@ def sum( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").sum() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 6.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").sum(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 6.0 nan Specify ``min_count`` for finer control over when NaNs are ignored. >>> ds.resample(time="3M").sum(skipna=True, min_count=2) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 nan 6.0 nan """ return self.reduce( duck_array_ops.sum, @@ -3127,16 +3758,41 @@ def std( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").std() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 0.0 0.8165 0.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").std(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 0.0 0.8165 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.resample(time="3M").std(skipna=True, ddof=1) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 nan 1.0 nan """ return self.reduce( duck_array_ops.std, @@ -3211,16 +3867,41 @@ def var( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").var() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 0.0 0.6667 0.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").var(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 0.0 0.6667 nan Specify ``ddof=1`` for an unbiased estimate. >>> ds.resample(time="3M").var(skipna=True, ddof=1) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 nan 1.0 nan """ return self.reduce( duck_array_ops.var, @@ -3291,12 +3972,31 @@ def median( ... ) >>> ds = xr.Dataset(dict(da=da)) >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.resample(time="3M").median() + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 2.0 2.0 Use ``skipna`` to control whether NaNs are ignored. >>> ds.resample(time="3M").median(skipna=False) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 1.0 2.0 nan """ return self.reduce( duck_array_ops.median, @@ -3371,8 +4071,17 @@ def count( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").count() + + array([1, 2, 2]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.count, @@ -3429,8 +4138,17 @@ def all( ... ), ... ) >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").all() + + array([False, True, True]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.array_all, @@ -3487,8 +4205,17 @@ def any( ... ), ... ) >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").any() + + array([ True, True, True]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.array_any, @@ -3551,12 +4278,25 @@ def max( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").max() + + array([1., 2., 3.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").max(skipna=False) + + array([nan, 2., 3.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.max, @@ -3620,12 +4360,25 @@ def min( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").min() + + array([1., 2., 1.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").min(skipna=False) + + array([nan, 2., 1.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.min, @@ -3693,12 +4446,25 @@ def mean( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").mean() + + array([1., 2., 2.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").mean(skipna=False) + + array([nan, 2., 2.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.mean, @@ -3773,16 +4539,33 @@ def prod( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").prod() + + array([1., 4., 3.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").prod(skipna=False) + + array([nan, 4., 3.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.groupby("labels").prod(skipna=True, min_count=2) + + array([nan, 4., 3.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.prod, @@ -3858,16 +4641,33 @@ def sum( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").sum() + + array([1., 4., 4.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").sum(skipna=False) + + array([nan, 4., 4.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.groupby("labels").sum(skipna=True, min_count=2) + + array([nan, 4., 4.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.sum, @@ -3940,16 +4740,33 @@ def std( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").std() + + array([0., 0., 1.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").std(skipna=False) + + array([nan, 0., 1.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Specify ``ddof=1`` for an unbiased estimate. >>> da.groupby("labels").std(skipna=True, ddof=1) + + array([ nan, 0. , 1.41421356]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.std, @@ -4022,16 +4839,33 @@ def var( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").var() + + array([0., 0., 1.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").var(skipna=False) + + array([nan, 0., 1.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Specify ``ddof=1`` for an unbiased estimate. >>> da.groupby("labels").var(skipna=True, ddof=1) + + array([nan, 0., 2.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.var, @@ -4100,12 +4934,25 @@ def median( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.groupby("labels").median() + + array([1., 2., 2.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' Use ``skipna`` to control whether NaNs are ignored. >>> da.groupby("labels").median(skipna=False) + + array([nan, 2., 2.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.median, @@ -4179,8 +5026,17 @@ def count( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").count() + + array([1, 3, 1]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.count, @@ -4237,8 +5093,17 @@ def all( ... ), ... ) >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").all() + + array([ True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.array_all, @@ -4295,8 +5160,17 @@ def any( ... ), ... ) >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").any() + + array([ True, True, True]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.array_any, @@ -4359,12 +5233,25 @@ def max( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").max() + + array([1., 3., 2.]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").max(skipna=False) + + array([ 1., 3., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.max, @@ -4428,12 +5315,25 @@ def min( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").min() + + array([1., 1., 2.]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").min(skipna=False) + + array([ 1., 1., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.min, @@ -4501,12 +5401,25 @@ def mean( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").mean() + + array([1., 2., 2.]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").mean(skipna=False) + + array([ 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.mean, @@ -4581,16 +5494,33 @@ def prod( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").prod() + + array([1., 6., 2.]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").prod(skipna=False) + + array([ 1., 6., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.resample(time="3M").prod(skipna=True, min_count=2) + + array([nan, 6., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.prod, @@ -4666,16 +5596,33 @@ def sum( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").sum() + + array([1., 6., 2.]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").sum(skipna=False) + + array([ 1., 6., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``min_count`` for finer control over when NaNs are ignored. >>> da.resample(time="3M").sum(skipna=True, min_count=2) + + array([nan, 6., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.sum, @@ -4748,16 +5695,33 @@ def std( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").std() + + array([0. , 0.81649658, 0. ]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").std(skipna=False) + + array([0. , 0.81649658, nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``ddof=1`` for an unbiased estimate. >>> da.resample(time="3M").std(skipna=True, ddof=1) + + array([nan, 1., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.std, @@ -4830,16 +5794,33 @@ def var( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").var() + + array([0. , 0.66666667, 0. ]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").var(skipna=False) + + array([0. , 0.66666667, nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Specify ``ddof=1`` for an unbiased estimate. >>> da.resample(time="3M").var(skipna=True, ddof=1) + + array([nan, 1., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.var, @@ -4908,12 +5889,25 @@ def median( ... ), ... ) >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.resample(time="3M").median() + + array([1., 2., 2.]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Use ``skipna`` to control whether NaNs are ignored. >>> da.resample(time="3M").median(skipna=False) + + array([ 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.median, From 6916fa7debfe4ca5c5ce9796fe5fe3243d6c4d2a Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Mon, 22 Nov 2021 11:16:43 -0700 Subject: [PATCH 20/31] Update xarray/util/generate_reductions.py Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com> --- xarray/util/generate_reductions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index fbf2d82dd2c..3a7f35ab567 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -44,7 +44,7 @@ def reduce( keepdims: bool = False, **kwargs: Any, ) -> "{obj}": - return NotImplemented""" + raise NotImplementedError()""" TEMPLATE_REDUCTION_SIGNATURE = ''' def {method}( From 2a1b12faf658bcd0079a33fe2d8bac4bc910d8e7 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Thu, 25 Nov 2021 21:41:21 -0700 Subject: [PATCH 21/31] Update xarray/util/generate_reductions.py Co-authored-by: Mathias Hauser --- xarray/util/generate_reductions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index 3a7f35ab567..9301c2e172d 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -80,7 +80,7 @@ def {method}( ----- {notes}""" -_DIM_DOCSTRING = """dim : hashable or iterable of hashable, optional +_DIM_DOCSTRING = """dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``{method}``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions.""" From 45feeaba8f3f19c8740da061a33839c5b2cf2ece Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Thu, 25 Nov 2021 21:52:06 -0700 Subject: [PATCH 22/31] Annotate some reduction tests. --- xarray/tests/test_dataarray.py | 4 ++-- xarray/tests/test_dataset.py | 26 +++++++++++++------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 5e9c1b87ce2..82c05acdec9 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -2392,7 +2392,7 @@ def test_cumops(self): expected = DataArray([[-1, 0, 0], [-3, 0, 0]], coords, dims=["x", "y"]) assert_identical(expected, actual) - def test_reduce(self): + def test_reduce(self) -> None: coords = { "x": [-1, -2], "y": ["ab", "cd", "ef"], @@ -2433,7 +2433,7 @@ def test_reduce(self): expected = DataArray(orig.values.astype(int), dims=["x", "y"]).mean("x") assert_equal(actual, expected) - def test_reduce_keepdims(self): + def test_reduce_keepdims(self) -> None: coords = { "x": [-1, -2], "y": ["ab", "cd", "ef"], diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index cdb8382c8ee..533496a7806 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -4432,7 +4432,7 @@ def test_where_drop_no_indexes(self): actual = ds.where(ds == 1, drop=True) assert_identical(expected, actual) - def test_reduce(self): + def test_reduce(self) -> None: data = create_test_data() assert len(data.mean().coords) == 0 @@ -4443,21 +4443,21 @@ def test_reduce(self): assert_equal(data.min(dim=["dim1"]), data.min(dim="dim1")) - for reduct, expected in [ + for reduct, expected_dims in [ ("dim2", ["dim3", "time", "dim1"]), (["dim2", "time"], ["dim3", "dim1"]), (("dim2", "time"), ["dim3", "dim1"]), ((), ["dim2", "dim3", "time", "dim1"]), ]: - actual = list(data.min(dim=reduct).dims) - assert actual == expected + actual_dims = list(data.min(dim=reduct).dims) + assert actual_dims == expected_dims assert_equal(data.mean(dim=[]), data) with pytest.raises(ValueError): data.mean(axis=0) - def test_reduce_coords(self): + def test_reduce_coords(self) -> None: # regression test for GH1470 data = xr.Dataset({"a": ("x", [1, 2, 3])}, coords={"b": 4}) expected = xr.Dataset({"a": 2}, coords={"b": 4}) @@ -4481,7 +4481,7 @@ def test_mean_uint_dtype(self): ) assert_identical(actual, expected) - def test_reduce_bad_dim(self): + def test_reduce_bad_dim(self) -> None: data = create_test_data() with pytest.raises(ValueError, match=r"Dataset does not contain"): data.mean(dim="bad_dim") @@ -4516,7 +4516,7 @@ def test_reduce_cumsum_test_dims(self, reduct, expected, func): actual = getattr(data, func)(dim=reduct).dims assert list(actual) == expected - def test_reduce_non_numeric(self): + def test_reduce_non_numeric(self) -> None: data1 = create_test_data(seed=44) data2 = create_test_data(seed=44) add_vars = {"var4": ["dim1", "dim2"], "var5": ["dim1"]} @@ -4533,7 +4533,7 @@ def test_reduce_non_numeric(self): @pytest.mark.filterwarnings( "ignore:Once the behaviour of DataArray:DeprecationWarning" ) - def test_reduce_strings(self): + def test_reduce_strings(self) -> None: expected = Dataset({"x": "a"}) ds = Dataset({"x": ("y", ["a", "b"])}) ds.coords["y"] = [-10, 10] @@ -4570,7 +4570,7 @@ def test_reduce_strings(self): actual = ds.min() assert_identical(expected, actual) - def test_reduce_dtypes(self): + def test_reduce_dtypes(self) -> None: # regression test for GH342 expected = Dataset({"x": 1}) actual = Dataset({"x": True}).sum() @@ -4585,7 +4585,7 @@ def test_reduce_dtypes(self): actual = Dataset({"x": ("y", [1, 1j])}).sum() assert_identical(expected, actual) - def test_reduce_keep_attrs(self): + def test_reduce_keep_attrs(self) -> None: data = create_test_data() _attrs = {"attr1": "value1", "attr2": 2929} @@ -4627,7 +4627,7 @@ def test_reduce_scalars(self): actual = ds.var("a") assert_identical(expected, actual) - def test_reduce_only_one_axis(self): + def test_reduce_only_one_axis(self) -> None: def mean_only_one_axis(x, axis): if not isinstance(axis, integer_types): raise TypeError("non-integer axis") @@ -4643,7 +4643,7 @@ def mean_only_one_axis(x, axis): ): ds.reduce(mean_only_one_axis) - def test_reduce_no_axis(self): + def test_reduce_no_axis(self) -> None: def total_sum(x): return np.sum(x.flatten()) @@ -4655,7 +4655,7 @@ def total_sum(x): with pytest.raises(TypeError, match=r"unexpected keyword argument 'axis'"): ds.reduce(total_sum, dim="x") - def test_reduce_keepdims(self): + def test_reduce_keepdims(self) -> None: ds = Dataset( {"a": (["x", "y"], [[0, 1, 2, 3, 4]])}, coords={ From 3dc94ae47165616878023b56a02b731549f920db Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Tue, 21 Dec 2021 21:42:00 +0100 Subject: [PATCH 23/31] force keyword args after dim --- xarray/util/generate_reductions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index fbf2d82dd2c..413c745194f 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -49,7 +49,8 @@ def reduce( TEMPLATE_REDUCTION_SIGNATURE = ''' def {method}( self, - dim: Union[None, Hashable, Sequence[Hashable]] = None,{extra_kwargs} + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *,{extra_kwargs} keep_attrs: bool = None, **kwargs, ) -> "{obj}": From bc55db33e63b10ef01e7c12518fcd7731e6d7d96 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Tue, 21 Dec 2021 22:48:57 +0100 Subject: [PATCH 24/31] Write to file using open() instead. --- xarray/util/generate_reductions.py | 32 ++++++++++++++++++------------ 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index 413c745194f..58335a11056 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -3,7 +3,7 @@ For internal xarray development use only. Usage: - python xarray/util/generate_reductions.py > xarray/core/_reductions.py + python xarray/util/generate_reductions.py pytest --doctest-modules xarray/core/_reductions.py --accept || true pytest --doctest-modules xarray/core/_reductions.py @@ -380,15 +380,21 @@ class DataStructure: if __name__ == "__main__": - print(MODULE_PREAMBLE) - for gen in [ - DatasetGenerator, - DataArrayGenerator, - DatasetGroupByGenerator, - DatasetResampleGenerator, - DataArrayGroupByGenerator, - DataArrayResampleGenerator, - ]: - for lines in gen.generate_methods(): - for line in lines: - print(line) + import os + from pathlib import Path + + p = Path(os.getcwd()) + filepath = p.parent / "core" / "_reductions.py" + with open(filepath, mode="w", encoding="utf-8") as f: + f.write(MODULE_PREAMBLE + "\n") + for gen in [ + DatasetGenerator, + DataArrayGenerator, + DatasetGroupByGenerator, + DatasetResampleGenerator, + DataArrayGroupByGenerator, + DataArrayResampleGenerator, + ]: + for lines in gen.generate_methods(): + for line in lines: + f.write(line + "\n") From b78df18bd11c496a4d5666c6972fb5d2f86e29d3 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Tue, 21 Dec 2021 22:49:07 +0100 Subject: [PATCH 25/31] Update _reductions.py --- xarray/core/_reductions.py | 72 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/xarray/core/_reductions.py b/xarray/core/_reductions.py index 5389aa4e09d..bd3d8e381d1 100644 --- a/xarray/core/_reductions.py +++ b/xarray/core/_reductions.py @@ -28,6 +28,7 @@ def reduce( def count( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "Dataset": @@ -86,7 +87,7 @@ def count( Dimensions: () Data variables: - da int64 5 + da int32 5 """ return self.reduce( duck_array_ops.count, @@ -99,6 +100,7 @@ def count( def all( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "Dataset": @@ -170,6 +172,7 @@ def all( def any( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "Dataset": @@ -241,6 +244,7 @@ def any( def max( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -327,6 +331,7 @@ def max( def min( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -413,6 +418,7 @@ def min( def mean( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -503,6 +509,7 @@ def mean( def prod( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, @@ -609,6 +616,7 @@ def prod( def sum( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, @@ -715,6 +723,7 @@ def sum( def std( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, @@ -818,6 +827,7 @@ def std( def var( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, @@ -921,6 +931,7 @@ def var( def median( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -1027,6 +1038,7 @@ def reduce( def count( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "DataArray": @@ -1092,6 +1104,7 @@ def count( def all( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "DataArray": @@ -1157,6 +1170,7 @@ def all( def any( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "DataArray": @@ -1222,6 +1236,7 @@ def any( def max( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -1300,6 +1315,7 @@ def max( def min( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -1378,6 +1394,7 @@ def min( def mean( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -1460,6 +1477,7 @@ def mean( def prod( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, @@ -1556,6 +1574,7 @@ def prod( def sum( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, @@ -1652,6 +1671,7 @@ def sum( def std( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, @@ -1745,6 +1765,7 @@ def std( def var( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, @@ -1838,6 +1859,7 @@ def var( def median( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -1936,6 +1958,7 @@ def reduce( def count( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "Dataset": @@ -1996,7 +2019,7 @@ def count( Coordinates: * labels (labels) object 'a' 'b' 'c' Data variables: - da (labels) int64 1 2 2 + da (labels) int32 1 2 2 """ return self.reduce( duck_array_ops.count, @@ -2009,6 +2032,7 @@ def count( def all( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "Dataset": @@ -2082,6 +2106,7 @@ def all( def any( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "Dataset": @@ -2155,6 +2180,7 @@ def any( def max( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -2245,6 +2271,7 @@ def max( def min( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -2335,6 +2362,7 @@ def min( def mean( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -2429,6 +2457,7 @@ def mean( def prod( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, @@ -2541,6 +2570,7 @@ def prod( def sum( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, @@ -2653,6 +2683,7 @@ def sum( def std( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, @@ -2762,6 +2793,7 @@ def std( def var( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, @@ -2871,6 +2903,7 @@ def var( def median( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -2981,6 +3014,7 @@ def reduce( def count( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "Dataset": @@ -3041,7 +3075,7 @@ def count( Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: - da (time) int64 1 3 1 + da (time) int32 1 3 1 """ return self.reduce( duck_array_ops.count, @@ -3054,6 +3088,7 @@ def count( def all( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "Dataset": @@ -3127,6 +3162,7 @@ def all( def any( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "Dataset": @@ -3200,6 +3236,7 @@ def any( def max( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -3290,6 +3327,7 @@ def max( def min( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -3380,6 +3418,7 @@ def min( def mean( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -3474,6 +3513,7 @@ def mean( def prod( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, @@ -3586,6 +3626,7 @@ def prod( def sum( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, @@ -3698,6 +3739,7 @@ def sum( def std( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, @@ -3807,6 +3849,7 @@ def std( def var( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, @@ -3916,6 +3959,7 @@ def var( def median( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -4026,6 +4070,7 @@ def reduce( def count( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "DataArray": @@ -4093,6 +4138,7 @@ def count( def all( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "DataArray": @@ -4160,6 +4206,7 @@ def all( def any( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "DataArray": @@ -4227,6 +4274,7 @@ def any( def max( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -4309,6 +4357,7 @@ def max( def min( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -4391,6 +4440,7 @@ def min( def mean( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -4477,6 +4527,7 @@ def mean( def prod( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, @@ -4579,6 +4630,7 @@ def prod( def sum( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, @@ -4681,6 +4733,7 @@ def sum( def std( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, @@ -4780,6 +4833,7 @@ def std( def var( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, @@ -4879,6 +4933,7 @@ def var( def median( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -4981,6 +5036,7 @@ def reduce( def count( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "DataArray": @@ -5048,6 +5104,7 @@ def count( def all( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "DataArray": @@ -5115,6 +5172,7 @@ def all( def any( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, ) -> "DataArray": @@ -5182,6 +5240,7 @@ def any( def max( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -5264,6 +5323,7 @@ def max( def min( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -5346,6 +5406,7 @@ def min( def mean( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, @@ -5432,6 +5493,7 @@ def mean( def prod( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, @@ -5534,6 +5596,7 @@ def prod( def sum( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, @@ -5636,6 +5699,7 @@ def sum( def std( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, @@ -5735,6 +5799,7 @@ def std( def var( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, ddof: int = 0, keep_attrs: bool = None, @@ -5834,6 +5899,7 @@ def var( def median( self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, skipna: bool = None, keep_attrs: bool = None, **kwargs, From 74064b957cb7c5d0dacec9708d17126fa11886bd Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Tue, 21 Dec 2021 23:03:05 +0100 Subject: [PATCH 26/31] manual tweaks to make ci happy --- xarray/core/_reductions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/core/_reductions.py b/xarray/core/_reductions.py index bd3d8e381d1..3aab793fdf2 100644 --- a/xarray/core/_reductions.py +++ b/xarray/core/_reductions.py @@ -87,7 +87,7 @@ def count( Dimensions: () Data variables: - da int32 5 + da int64 5 """ return self.reduce( duck_array_ops.count, @@ -2019,7 +2019,7 @@ def count( Coordinates: * labels (labels) object 'a' 'b' 'c' Data variables: - da (labels) int32 1 2 2 + da (labels) int64 1 2 2 """ return self.reduce( duck_array_ops.count, @@ -3075,7 +3075,7 @@ def count( Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: - da (time) int32 1 3 1 + da (time) int64 1 3 1 """ return self.reduce( duck_array_ops.count, From 7b34077c1355d006155abf3cdb5bc289caae96a9 Mon Sep 17 00:00:00 2001 From: dcherian Date: Wed, 16 Feb 2022 09:50:49 -0700 Subject: [PATCH 27/31] Fix path --- xarray/core/_reductions.py | 144 ++++++++++++++--------------- xarray/util/generate_reductions.py | 2 +- 2 files changed, 73 insertions(+), 73 deletions(-) diff --git a/xarray/core/_reductions.py b/xarray/core/_reductions.py index 3aab793fdf2..31365f39e65 100644 --- a/xarray/core/_reductions.py +++ b/xarray/core/_reductions.py @@ -23,7 +23,7 @@ def reduce( keepdims: bool = False, **kwargs: Any, ) -> "Dataset": - return NotImplemented + raise NotImplementedError() def count( self, @@ -37,7 +37,7 @@ def count( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -109,7 +109,7 @@ def all( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -181,7 +181,7 @@ def any( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -254,7 +254,7 @@ def max( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -341,7 +341,7 @@ def min( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -428,7 +428,7 @@ def mean( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -520,7 +520,7 @@ def prod( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -627,7 +627,7 @@ def sum( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -734,7 +734,7 @@ def std( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -838,7 +838,7 @@ def var( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -941,7 +941,7 @@ def median( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -1033,7 +1033,7 @@ def reduce( keepdims: bool = False, **kwargs: Any, ) -> "DataArray": - return NotImplemented + raise NotImplementedError() def count( self, @@ -1047,7 +1047,7 @@ def count( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -1113,7 +1113,7 @@ def all( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -1179,7 +1179,7 @@ def any( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -1246,7 +1246,7 @@ def max( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -1325,7 +1325,7 @@ def min( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -1404,7 +1404,7 @@ def mean( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -1488,7 +1488,7 @@ def prod( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -1585,7 +1585,7 @@ def sum( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -1682,7 +1682,7 @@ def std( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -1776,7 +1776,7 @@ def var( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -1869,7 +1869,7 @@ def median( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -1953,7 +1953,7 @@ def reduce( keepdims: bool = False, **kwargs: Any, ) -> "Dataset": - return NotImplemented + raise NotImplementedError() def count( self, @@ -1967,7 +1967,7 @@ def count( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -2041,7 +2041,7 @@ def all( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -2115,7 +2115,7 @@ def any( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -2190,7 +2190,7 @@ def max( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -2281,7 +2281,7 @@ def min( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -2372,7 +2372,7 @@ def mean( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -2468,7 +2468,7 @@ def prod( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -2581,7 +2581,7 @@ def sum( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -2694,7 +2694,7 @@ def std( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -2804,7 +2804,7 @@ def var( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -2913,7 +2913,7 @@ def median( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -3009,7 +3009,7 @@ def reduce( keepdims: bool = False, **kwargs: Any, ) -> "Dataset": - return NotImplemented + raise NotImplementedError() def count( self, @@ -3023,7 +3023,7 @@ def count( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -3097,7 +3097,7 @@ def all( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -3171,7 +3171,7 @@ def any( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -3246,7 +3246,7 @@ def max( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -3337,7 +3337,7 @@ def min( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -3428,7 +3428,7 @@ def mean( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -3524,7 +3524,7 @@ def prod( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -3637,7 +3637,7 @@ def sum( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -3750,7 +3750,7 @@ def std( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -3860,7 +3860,7 @@ def var( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -3969,7 +3969,7 @@ def median( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -4065,7 +4065,7 @@ def reduce( keepdims: bool = False, **kwargs: Any, ) -> "DataArray": - return NotImplemented + raise NotImplementedError() def count( self, @@ -4079,7 +4079,7 @@ def count( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -4147,7 +4147,7 @@ def all( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -4215,7 +4215,7 @@ def any( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -4284,7 +4284,7 @@ def max( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -4367,7 +4367,7 @@ def min( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -4450,7 +4450,7 @@ def mean( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -4538,7 +4538,7 @@ def prod( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -4641,7 +4641,7 @@ def sum( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -4744,7 +4744,7 @@ def std( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -4844,7 +4844,7 @@ def var( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -4943,7 +4943,7 @@ def median( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -5031,7 +5031,7 @@ def reduce( keepdims: bool = False, **kwargs: Any, ) -> "DataArray": - return NotImplemented + raise NotImplementedError() def count( self, @@ -5045,7 +5045,7 @@ def count( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -5113,7 +5113,7 @@ def all( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -5181,7 +5181,7 @@ def any( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional @@ -5250,7 +5250,7 @@ def max( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -5333,7 +5333,7 @@ def min( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -5416,7 +5416,7 @@ def mean( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -5504,7 +5504,7 @@ def prod( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -5607,7 +5607,7 @@ def sum( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -5710,7 +5710,7 @@ def std( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -5810,7 +5810,7 @@ def var( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None @@ -5909,7 +5909,7 @@ def median( Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` or ``dim=["x", "y"]``. If None, will reduce over all dimensions. skipna : bool, default: None diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index 9382ce17dab..6dc1bc04cec 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -384,7 +384,7 @@ class DataStructure: from pathlib import Path p = Path(os.getcwd()) - filepath = p.parent / "core" / "_reductions.py" + filepath = p.parent / "xarray" / "xarray" / "core" / "_reductions.py" with open(filepath, mode="w", encoding="utf-8") as f: f.write(MODULE_PREAMBLE + "\n") for gen in [ From 9799d87f1e45f2e20a55bab13af63f2d895666ee Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Tue, 8 Mar 2022 16:57:55 -0700 Subject: [PATCH 28/31] Apply suggestions from code review Co-authored-by: Stephan Hoyer --- xarray/util/generate_reductions.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index 6dc1bc04cec..95b2fc640f9 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -120,7 +120,7 @@ def {method}( ] _NUMERIC_ONLY_NOTES = "Non-numeric variables will be removed prior to reducing." -extra_kwarg = collections.namedtuple("extra_kwarg", "docs kwarg call example") +ExtraKwarg = collections.namedtuple("ExtraKwarg", "docs kwarg call example") skipna = extra_kwarg( docs=_SKIPNA_DOCSTRING, kwarg="skipna: bool = None,", @@ -279,7 +279,8 @@ def generate_code(self, method): extra_kwargs = textwrap.indent("\n" + "\n".join(extra_kwargs), 12 * " ") else: extra_kwargs = "" - return f""" return self.reduce( + return f"""\ + return self.reduce( duck_array_ops.{method.array_method}, dim=dim,{extra_kwargs} keep_attrs=keep_attrs, @@ -310,7 +311,7 @@ class DataStructure: numeric_only: bool = False -DatasetObject = DataStructure( +DATASET_OBJECT = DataStructure( name="Dataset", docstring_create=""" >>> ds = xr.Dataset(dict(da=da)) @@ -326,7 +327,7 @@ class DataStructure: numeric_only=False, ) -DatasetGenerator = GenericReductionGenerator( +DATASET_GENERATOR = GenericReductionGenerator( cls="", datastructure=DatasetObject, methods=REDUCTION_METHODS, From 1fcd0808d3f925ecd5bb4f03c94d70136a332487 Mon Sep 17 00:00:00 2001 From: dcherian Date: Tue, 8 Mar 2022 16:59:43 -0700 Subject: [PATCH 29/31] Fixes --- xarray/util/generate_reductions.py | 42 +++++++++++++++--------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index 95b2fc640f9..e79c94e8907 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -121,7 +121,7 @@ def {method}( _NUMERIC_ONLY_NOTES = "Non-numeric variables will be removed prior to reducing." ExtraKwarg = collections.namedtuple("ExtraKwarg", "docs kwarg call example") -skipna = extra_kwarg( +skipna = ExtraKwarg( docs=_SKIPNA_DOCSTRING, kwarg="skipna: bool = None,", call="skipna=skipna,", @@ -130,7 +130,7 @@ def {method}( >>> {calculation}(skipna=False)""", ) -min_count = extra_kwarg( +min_count = ExtraKwarg( docs=_MINCOUNT_DOCSTRING, kwarg="min_count: Optional[int] = None,", call="min_count=min_count,", @@ -139,7 +139,7 @@ def {method}( >>> {calculation}(skipna=True, min_count=2)""", ) -ddof = extra_kwarg( +ddof = ExtraKwarg( docs=_DDOF_DOCSTRING, kwarg="ddof: int = 0,", call="ddof=ddof,", @@ -319,7 +319,7 @@ class DataStructure: example_var_name="ds", numeric_only=True, ) -DataArrayObject = DataStructure( +DATAARRAY_OBJECT = DataStructure( name="DataArray", docstring_create=""" >>> da""", @@ -329,16 +329,16 @@ class DataStructure: DATASET_GENERATOR = GenericReductionGenerator( cls="", - datastructure=DatasetObject, + datastructure=DATASET_OBJECT, methods=REDUCTION_METHODS, docref="agg", docref_description="reduction or aggregation operations", example_call_preamble="", see_also_obj="DataArray", ) -DataArrayGenerator = GenericReductionGenerator( +DATAARRAY_GENERATOR = GenericReductionGenerator( cls="", - datastructure=DataArrayObject, + datastructure=DATAARRAY_OBJECT, methods=REDUCTION_METHODS, docref="agg", docref_description="reduction or aggregation operations", @@ -346,33 +346,33 @@ class DataStructure: see_also_obj="Dataset", ) -DataArrayGroupByGenerator = GenericReductionGenerator( +DATAARRAY_GROUPBY_GENERATOR = GenericReductionGenerator( cls="GroupBy", - datastructure=DataArrayObject, + datastructure=DATAARRAY_OBJECT, methods=REDUCTION_METHODS, docref="groupby", docref_description="groupby operations", example_call_preamble='.groupby("labels")', ) -DataArrayResampleGenerator = GenericReductionGenerator( +DATAARRAY_RESAMPLE_GENERATOR = GenericReductionGenerator( cls="Resample", - datastructure=DataArrayObject, + datastructure=DATAARRAY_OBJECT, methods=REDUCTION_METHODS, docref="resampling", docref_description="resampling operations", example_call_preamble='.resample(time="3M")', ) -DatasetGroupByGenerator = GenericReductionGenerator( +DATASET_GROUPBY_GENERATOR = GenericReductionGenerator( cls="GroupBy", - datastructure=DatasetObject, + datastructure=DATASET_OBJECT, methods=REDUCTION_METHODS, docref="groupby", docref_description="groupby operations", example_call_preamble='.groupby("labels")', ) -DatasetResampleGenerator = GenericReductionGenerator( +DATASET_RESAMPLE_GENERATOR = GenericReductionGenerator( cls="Resample", - datastructure=DatasetObject, + datastructure=DATASET_OBJECT, methods=REDUCTION_METHODS, docref="resampling", docref_description="resampling operations", @@ -389,12 +389,12 @@ class DataStructure: with open(filepath, mode="w", encoding="utf-8") as f: f.write(MODULE_PREAMBLE + "\n") for gen in [ - DatasetGenerator, - DataArrayGenerator, - DatasetGroupByGenerator, - DatasetResampleGenerator, - DataArrayGroupByGenerator, - DataArrayResampleGenerator, + DATASET_GENERATOR, + DATAARRAY_GENERATOR, + DATASET_GROUPBY_GENERATOR, + DATASET_RESAMPLE_GENERATOR, + DATAARRAY_GROUPBY_GENERATOR, + DATAARRAY_RESAMPLE_GENERATOR, ]: for lines in gen.generate_methods(): for line in lines: From d5f627cf67204b78957a3bbc6147c455e54d080b Mon Sep 17 00:00:00 2001 From: dcherian Date: Tue, 8 Mar 2022 17:48:00 -0700 Subject: [PATCH 30/31] update _reductions --- xarray/core/_reductions.py | 3527 +++++++++++++++++++++++----- xarray/util/generate_reductions.py | 1 + 2 files changed, 2890 insertions(+), 638 deletions(-) diff --git a/xarray/core/_reductions.py b/xarray/core/_reductions.py index 83aaa10a20c..31365f39e65 100644 --- a/xarray/core/_reductions.py +++ b/xarray/core/_reductions.py @@ -1,43 +1,1975 @@ """Mixin classes with reduction operations.""" # This file was generated using xarray.util.generate_reductions. Do not edit manually. -from typing import Any, Callable, Hashable, Optional, Protocol, Sequence, Union +from typing import TYPE_CHECKING, Any, Callable, Hashable, Optional, Sequence, Union from . import duck_array_ops -from .types import T_DataArray, T_Dataset +if TYPE_CHECKING: + from .dataarray import DataArray + from .dataset import Dataset + + +class DatasetReductions: + __slots__ = () + + def reduce( + self, + func: Callable[..., Any], + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + axis: Union[None, int, Sequence[int]] = None, + keep_attrs: bool = None, + keepdims: bool = False, + **kwargs: Any, + ) -> "Dataset": + raise NotImplementedError() + + def count( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + keep_attrs: bool = None, + **kwargs, + ) -> "Dataset": + """ + Reduce this Dataset's data by applying ``count`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``count`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : Dataset + New Dataset with ``count`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.count + dask.array.count + DataArray.count + :ref:`agg` + User guide on reduction or aggregation operations. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.count() + + Dimensions: () + Data variables: + da int64 5 + """ + return self.reduce( + duck_array_ops.count, + dim=dim, + numeric_only=False, + keep_attrs=keep_attrs, + **kwargs, + ) + + def all( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + keep_attrs: bool = None, + **kwargs, + ) -> "Dataset": + """ + Reduce this Dataset's data by applying ``all`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``all`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : Dataset + New Dataset with ``all`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.all + dask.array.all + DataArray.all + :ref:`agg` + User guide on reduction or aggregation operations. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([True, True, True, True, True, False], dtype=bool), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.all() + + Dimensions: () + Data variables: + da bool False + """ + return self.reduce( + duck_array_ops.array_all, + dim=dim, + numeric_only=False, + keep_attrs=keep_attrs, + **kwargs, + ) + + def any( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + keep_attrs: bool = None, + **kwargs, + ) -> "Dataset": + """ + Reduce this Dataset's data by applying ``any`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``any`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : Dataset + New Dataset with ``any`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.any + dask.array.any + DataArray.any + :ref:`agg` + User guide on reduction or aggregation operations. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([True, True, True, True, True, False], dtype=bool), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.any() + + Dimensions: () + Data variables: + da bool True + """ + return self.reduce( + duck_array_ops.array_any, + dim=dim, + numeric_only=False, + keep_attrs=keep_attrs, + **kwargs, + ) + + def max( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> "Dataset": + """ + Reduce this Dataset's data by applying ``max`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``max`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : Dataset + New Dataset with ``max`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.max + dask.array.max + DataArray.max + :ref:`agg` + User guide on reduction or aggregation operations. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.max() + + Dimensions: () + Data variables: + da float64 3.0 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.max(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + """ + return self.reduce( + duck_array_ops.max, + dim=dim, + skipna=skipna, + numeric_only=False, + keep_attrs=keep_attrs, + **kwargs, + ) + + def min( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> "Dataset": + """ + Reduce this Dataset's data by applying ``min`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``min`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : Dataset + New Dataset with ``min`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.min + dask.array.min + DataArray.min + :ref:`agg` + User guide on reduction or aggregation operations. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.min() + + Dimensions: () + Data variables: + da float64 1.0 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.min(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + """ + return self.reduce( + duck_array_ops.min, + dim=dim, + skipna=skipna, + numeric_only=False, + keep_attrs=keep_attrs, + **kwargs, + ) + + def mean( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> "Dataset": + """ + Reduce this Dataset's data by applying ``mean`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``mean`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : Dataset + New Dataset with ``mean`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.mean + dask.array.mean + DataArray.mean + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.mean() + + Dimensions: () + Data variables: + da float64 1.8 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.mean(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + """ + return self.reduce( + duck_array_ops.mean, + dim=dim, + skipna=skipna, + numeric_only=True, + keep_attrs=keep_attrs, + **kwargs, + ) + + def prod( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + min_count: Optional[int] = None, + keep_attrs: bool = None, + **kwargs, + ) -> "Dataset": + """ + Reduce this Dataset's data by applying ``prod`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + min_count : int, default: None + The required number of valid values to perform the operation. If + fewer than min_count non-NA values are present the result will be + NA. Only used if skipna is set to True or defaults to True for the + array's dtype. Changed in version 0.17.0: if specified on an integer + array and skipna=True, the result will be a float array. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``prod`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : Dataset + New Dataset with ``prod`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.prod + dask.array.prod + DataArray.prod + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.prod() + + Dimensions: () + Data variables: + da float64 12.0 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.prod(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + + Specify ``min_count`` for finer control over when NaNs are ignored. + + >>> ds.prod(skipna=True, min_count=2) + + Dimensions: () + Data variables: + da float64 12.0 + """ + return self.reduce( + duck_array_ops.prod, + dim=dim, + skipna=skipna, + min_count=min_count, + numeric_only=True, + keep_attrs=keep_attrs, + **kwargs, + ) + + def sum( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + min_count: Optional[int] = None, + keep_attrs: bool = None, + **kwargs, + ) -> "Dataset": + """ + Reduce this Dataset's data by applying ``sum`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + min_count : int, default: None + The required number of valid values to perform the operation. If + fewer than min_count non-NA values are present the result will be + NA. Only used if skipna is set to True or defaults to True for the + array's dtype. Changed in version 0.17.0: if specified on an integer + array and skipna=True, the result will be a float array. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``sum`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : Dataset + New Dataset with ``sum`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.sum + dask.array.sum + DataArray.sum + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.sum() + + Dimensions: () + Data variables: + da float64 9.0 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.sum(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + + Specify ``min_count`` for finer control over when NaNs are ignored. + + >>> ds.sum(skipna=True, min_count=2) + + Dimensions: () + Data variables: + da float64 9.0 + """ + return self.reduce( + duck_array_ops.sum, + dim=dim, + skipna=skipna, + min_count=min_count, + numeric_only=True, + keep_attrs=keep_attrs, + **kwargs, + ) + + def std( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + ddof: int = 0, + keep_attrs: bool = None, + **kwargs, + ) -> "Dataset": + """ + Reduce this Dataset's data by applying ``std`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``std`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : Dataset + New Dataset with ``std`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.std + dask.array.std + DataArray.std + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.std() + + Dimensions: () + Data variables: + da float64 0.7483 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.std(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + + Specify ``ddof=1`` for an unbiased estimate. + + >>> ds.std(skipna=True, ddof=1) + + Dimensions: () + Data variables: + da float64 0.8367 + """ + return self.reduce( + duck_array_ops.std, + dim=dim, + skipna=skipna, + ddof=ddof, + numeric_only=True, + keep_attrs=keep_attrs, + **kwargs, + ) + + def var( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + ddof: int = 0, + keep_attrs: bool = None, + **kwargs, + ) -> "Dataset": + """ + Reduce this Dataset's data by applying ``var`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``var`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : Dataset + New Dataset with ``var`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.var + dask.array.var + DataArray.var + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.var() + + Dimensions: () + Data variables: + da float64 0.56 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.var(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + + Specify ``ddof=1`` for an unbiased estimate. + + >>> ds.var(skipna=True, ddof=1) + + Dimensions: () + Data variables: + da float64 0.7 + """ + return self.reduce( + duck_array_ops.var, + dim=dim, + skipna=skipna, + ddof=ddof, + numeric_only=True, + keep_attrs=keep_attrs, + **kwargs, + ) + + def median( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> "Dataset": + """ + Reduce this Dataset's data by applying ``median`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``median`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : Dataset + New Dataset with ``median`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.median + dask.array.median + DataArray.median + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> ds = xr.Dataset(dict(da=da)) + >>> ds + + Dimensions: (time: 6) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> ds.median() + + Dimensions: () + Data variables: + da float64 2.0 + + Use ``skipna`` to control whether NaNs are ignored. + + >>> ds.median(skipna=False) + + Dimensions: () + Data variables: + da float64 nan + """ + return self.reduce( + duck_array_ops.median, + dim=dim, + skipna=skipna, + numeric_only=True, + keep_attrs=keep_attrs, + **kwargs, + ) + + +class DataArrayReductions: + __slots__ = () + + def reduce( + self, + func: Callable[..., Any], + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + axis: Union[None, int, Sequence[int]] = None, + keep_attrs: bool = None, + keepdims: bool = False, + **kwargs: Any, + ) -> "DataArray": + raise NotImplementedError() + + def count( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + keep_attrs: bool = None, + **kwargs, + ) -> "DataArray": + """ + Reduce this DataArray's data by applying ``count`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``count`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : DataArray + New DataArray with ``count`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.count + dask.array.count + Dataset.count + :ref:`agg` + User guide on reduction or aggregation operations. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.count() + + array(5) + """ + return self.reduce( + duck_array_ops.count, + dim=dim, + keep_attrs=keep_attrs, + **kwargs, + ) + + def all( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + keep_attrs: bool = None, + **kwargs, + ) -> "DataArray": + """ + Reduce this DataArray's data by applying ``all`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``all`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : DataArray + New DataArray with ``all`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.all + dask.array.all + Dataset.all + :ref:`agg` + User guide on reduction or aggregation operations. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([True, True, True, True, True, False], dtype=bool), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.all() + + array(False) + """ + return self.reduce( + duck_array_ops.array_all, + dim=dim, + keep_attrs=keep_attrs, + **kwargs, + ) + + def any( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + keep_attrs: bool = None, + **kwargs, + ) -> "DataArray": + """ + Reduce this DataArray's data by applying ``any`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``any`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : DataArray + New DataArray with ``any`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.any + dask.array.any + Dataset.any + :ref:`agg` + User guide on reduction or aggregation operations. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([True, True, True, True, True, False], dtype=bool), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ True, True, True, True, True, False]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.any() + + array(True) + """ + return self.reduce( + duck_array_ops.array_any, + dim=dim, + keep_attrs=keep_attrs, + **kwargs, + ) + + def max( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> "DataArray": + """ + Reduce this DataArray's data by applying ``max`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``max`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : DataArray + New DataArray with ``max`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.max + dask.array.max + Dataset.max + :ref:`agg` + User guide on reduction or aggregation operations. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.max() + + array(3.) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.max(skipna=False) + + array(nan) + """ + return self.reduce( + duck_array_ops.max, + dim=dim, + skipna=skipna, + keep_attrs=keep_attrs, + **kwargs, + ) + + def min( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> "DataArray": + """ + Reduce this DataArray's data by applying ``min`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``min`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : DataArray + New DataArray with ``min`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.min + dask.array.min + Dataset.min + :ref:`agg` + User guide on reduction or aggregation operations. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.min() + + array(1.) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.min(skipna=False) + + array(nan) + """ + return self.reduce( + duck_array_ops.min, + dim=dim, + skipna=skipna, + keep_attrs=keep_attrs, + **kwargs, + ) + + def mean( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> "DataArray": + """ + Reduce this DataArray's data by applying ``mean`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``mean`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : DataArray + New DataArray with ``mean`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.mean + dask.array.mean + Dataset.mean + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.mean() + + array(1.8) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.mean(skipna=False) + + array(nan) + """ + return self.reduce( + duck_array_ops.mean, + dim=dim, + skipna=skipna, + keep_attrs=keep_attrs, + **kwargs, + ) + + def prod( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + min_count: Optional[int] = None, + keep_attrs: bool = None, + **kwargs, + ) -> "DataArray": + """ + Reduce this DataArray's data by applying ``prod`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + min_count : int, default: None + The required number of valid values to perform the operation. If + fewer than min_count non-NA values are present the result will be + NA. Only used if skipna is set to True or defaults to True for the + array's dtype. Changed in version 0.17.0: if specified on an integer + array and skipna=True, the result will be a float array. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``prod`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : DataArray + New DataArray with ``prod`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.prod + dask.array.prod + Dataset.prod + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.prod() + + array(12.) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.prod(skipna=False) + + array(nan) + + Specify ``min_count`` for finer control over when NaNs are ignored. + + >>> da.prod(skipna=True, min_count=2) + + array(12.) + """ + return self.reduce( + duck_array_ops.prod, + dim=dim, + skipna=skipna, + min_count=min_count, + keep_attrs=keep_attrs, + **kwargs, + ) + + def sum( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + min_count: Optional[int] = None, + keep_attrs: bool = None, + **kwargs, + ) -> "DataArray": + """ + Reduce this DataArray's data by applying ``sum`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + min_count : int, default: None + The required number of valid values to perform the operation. If + fewer than min_count non-NA values are present the result will be + NA. Only used if skipna is set to True or defaults to True for the + array's dtype. Changed in version 0.17.0: if specified on an integer + array and skipna=True, the result will be a float array. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``sum`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : DataArray + New DataArray with ``sum`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.sum + dask.array.sum + Dataset.sum + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.sum() + + array(9.) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.sum(skipna=False) + + array(nan) + + Specify ``min_count`` for finer control over when NaNs are ignored. + + >>> da.sum(skipna=True, min_count=2) + + array(9.) + """ + return self.reduce( + duck_array_ops.sum, + dim=dim, + skipna=skipna, + min_count=min_count, + keep_attrs=keep_attrs, + **kwargs, + ) + + def std( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + ddof: int = 0, + keep_attrs: bool = None, + **kwargs, + ) -> "DataArray": + """ + Reduce this DataArray's data by applying ``std`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``std`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : DataArray + New DataArray with ``std`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.std + dask.array.std + Dataset.std + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.std() + + array(0.74833148) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.std(skipna=False) + + array(nan) + + Specify ``ddof=1`` for an unbiased estimate. + + >>> da.std(skipna=True, ddof=1) + + array(0.83666003) + """ + return self.reduce( + duck_array_ops.std, + dim=dim, + skipna=skipna, + ddof=ddof, + keep_attrs=keep_attrs, + **kwargs, + ) + + def var( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + ddof: int = 0, + keep_attrs: bool = None, + **kwargs, + ) -> "DataArray": + """ + Reduce this DataArray's data by applying ``var`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``var`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : DataArray + New DataArray with ``var`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.var + dask.array.var + Dataset.var + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.var() + + array(0.56) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.var(skipna=False) + + array(nan) + + Specify ``ddof=1`` for an unbiased estimate. + + >>> da.var(skipna=True, ddof=1) + + array(0.7) + """ + return self.reduce( + duck_array_ops.var, + dim=dim, + skipna=skipna, + ddof=ddof, + keep_attrs=keep_attrs, + **kwargs, + ) + + def median( + self, + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + skipna: bool = None, + keep_attrs: bool = None, + **kwargs, + ) -> "DataArray": + """ + Reduce this DataArray's data by applying ``median`` along some dimension(s). + + Parameters + ---------- + dim : hashable or iterable of hashable, default: None + Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or ``skipna=True`` has not been + implemented (object, datetime64 or timedelta64). + keep_attrs : bool, optional + If True, ``attrs`` will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + **kwargs : dict + Additional keyword arguments passed on to the appropriate array + function for calculating ``median`` on this object's data. + These could include dask-specific kwargs like ``split_every``. + + Returns + ------- + reduced : DataArray + New DataArray with ``median`` applied to its data and the + indicated dimension(s) removed + + See Also + -------- + numpy.median + dask.array.median + Dataset.median + :ref:`agg` + User guide on reduction or aggregation operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + + Examples + -------- + >>> da = xr.DataArray( + ... np.array([1, 2, 3, 1, 2, np.nan]), + ... dims="time", + ... coords=dict( + ... time=("time", pd.date_range("01-01-2001", freq="M", periods=6)), + ... labels=("time", np.array(["a", "b", "c", "c", "b", "a"])), + ... ), + ... ) + >>> da + + array([ 1., 2., 3., 1., 2., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-02-28 ... 2001-06-30 + labels (time) >> da.median() + + array(2.) + + Use ``skipna`` to control whether NaNs are ignored. + + >>> da.median(skipna=False) + + array(nan) + """ + return self.reduce( + duck_array_ops.median, + dim=dim, + skipna=skipna, + keep_attrs=keep_attrs, + **kwargs, + ) + + +class DatasetGroupByReductions: + __slots__ = () -class DatasetReduce(Protocol): def reduce( self, func: Callable[..., Any], dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, axis: Union[None, int, Sequence[int]] = None, keep_attrs: bool = None, keepdims: bool = False, **kwargs: Any, - ) -> T_Dataset: - ... - - -class DatasetGroupByReductions: - __slots__ = () + ) -> "Dataset": + raise NotImplementedError() def count( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``count`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -45,6 +1977,7 @@ def count( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``count`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -52,6 +1985,14 @@ def count( New Dataset with ``count`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.count + dask.array.count + Dataset.count + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -79,13 +2020,6 @@ def count( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) int64 1 2 2 - - See Also - -------- - numpy.count - Dataset.count - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.count, @@ -96,20 +2030,20 @@ def count( ) def all( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``all`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -117,6 +2051,7 @@ def all( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``all`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -124,6 +2059,14 @@ def all( New Dataset with ``all`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.all + dask.array.all + Dataset.all + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -151,13 +2094,6 @@ def all( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) bool False True True - - See Also - -------- - numpy.all - Dataset.all - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.array_all, @@ -168,20 +2104,20 @@ def all( ) def any( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``any`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -189,6 +2125,7 @@ def any( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``any`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -196,6 +2133,14 @@ def any( New Dataset with ``any`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.any + dask.array.any + Dataset.any + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -223,13 +2168,6 @@ def any( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) bool True True True - - See Also - -------- - numpy.any - Dataset.any - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.array_any, @@ -240,25 +2178,25 @@ def any( ) def max( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``max`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -267,6 +2205,7 @@ def max( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``max`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -274,6 +2213,14 @@ def max( New Dataset with ``max`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.max + dask.array.max + Dataset.max + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -311,13 +2258,6 @@ def max( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) float64 nan 2.0 3.0 - - See Also - -------- - numpy.max - Dataset.max - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.max, @@ -329,25 +2269,25 @@ def max( ) def min( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``min`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -356,6 +2296,7 @@ def min( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``min`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -363,6 +2304,14 @@ def min( New Dataset with ``min`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.min + dask.array.min + Dataset.min + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -400,13 +2349,6 @@ def min( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) float64 nan 2.0 1.0 - - See Also - -------- - numpy.min - Dataset.min - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.min, @@ -418,25 +2360,25 @@ def min( ) def mean( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``mean`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -445,6 +2387,7 @@ def mean( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``mean`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -452,6 +2395,18 @@ def mean( New Dataset with ``mean`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.mean + dask.array.mean + Dataset.mean + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -489,13 +2444,6 @@ def mean( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) float64 nan 2.0 2.0 - - See Also - -------- - numpy.mean - Dataset.mean - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.mean, @@ -507,26 +2455,26 @@ def mean( ) def prod( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``prod`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -541,6 +2489,7 @@ def prod( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``prod`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -548,6 +2497,18 @@ def prod( New Dataset with ``prod`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.prod + dask.array.prod + Dataset.prod + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -595,13 +2556,6 @@ def prod( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) float64 nan 4.0 3.0 - - See Also - -------- - numpy.prod - Dataset.prod - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.prod, @@ -614,26 +2568,26 @@ def prod( ) def sum( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``sum`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -648,6 +2602,7 @@ def sum( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``sum`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -655,6 +2610,18 @@ def sum( New Dataset with ``sum`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.sum + dask.array.sum + Dataset.sum + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -702,13 +2669,6 @@ def sum( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) float64 nan 4.0 4.0 - - See Also - -------- - numpy.sum - Dataset.sum - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.sum, @@ -721,26 +2681,30 @@ def sum( ) def std( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, + ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``std`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -748,6 +2712,7 @@ def std( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``std`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -755,6 +2720,18 @@ def std( New Dataset with ``std`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.std + dask.array.std + Dataset.std + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -793,43 +2770,51 @@ def std( Data variables: da (labels) float64 nan 0.0 1.0 - See Also - -------- - numpy.std - Dataset.std - :ref:`groupby` - User guide on groupby operations. + Specify ``ddof=1`` for an unbiased estimate. + + >>> ds.groupby("labels").std(skipna=True, ddof=1) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 0.0 1.414 """ return self.reduce( duck_array_ops.std, dim=dim, skipna=skipna, + ddof=ddof, numeric_only=True, keep_attrs=keep_attrs, **kwargs, ) def var( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, + ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``var`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -837,6 +2822,7 @@ def var( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``var`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -844,6 +2830,18 @@ def var( New Dataset with ``var`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.var + dask.array.var + Dataset.var + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -882,42 +2880,46 @@ def var( Data variables: da (labels) float64 nan 0.0 1.0 - See Also - -------- - numpy.var - Dataset.var - :ref:`groupby` - User guide on groupby operations. + Specify ``ddof=1`` for an unbiased estimate. + + >>> ds.groupby("labels").var(skipna=True, ddof=1) + + Dimensions: (labels: 3) + Coordinates: + * labels (labels) object 'a' 'b' 'c' + Data variables: + da (labels) float64 nan 0.0 2.0 """ return self.reduce( duck_array_ops.var, dim=dim, skipna=skipna, + ddof=ddof, numeric_only=True, keep_attrs=keep_attrs, **kwargs, ) def median( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``median`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -926,6 +2928,7 @@ def median( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``median`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -933,6 +2936,18 @@ def median( New Dataset with ``median`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.median + dask.array.median + Dataset.median + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -970,13 +2985,6 @@ def median( * labels (labels) object 'a' 'b' 'c' Data variables: da (labels) float64 nan 2.0 2.0 - - See Also - -------- - numpy.median - Dataset.median - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.median, @@ -991,21 +2999,33 @@ def median( class DatasetResampleReductions: __slots__ = () + def reduce( + self, + func: Callable[..., Any], + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + axis: Union[None, int, Sequence[int]] = None, + keep_attrs: bool = None, + keepdims: bool = False, + **kwargs: Any, + ) -> "Dataset": + raise NotImplementedError() + def count( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``count`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -1013,6 +3033,7 @@ def count( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``count`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1020,6 +3041,14 @@ def count( New Dataset with ``count`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.count + dask.array.count + Dataset.count + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -1047,13 +3076,6 @@ def count( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) int64 1 3 1 - - See Also - -------- - numpy.count - Dataset.count - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.count, @@ -1064,20 +3086,20 @@ def count( ) def all( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``all`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -1085,6 +3107,7 @@ def all( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``all`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1092,6 +3115,14 @@ def all( New Dataset with ``all`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.all + dask.array.all + Dataset.all + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -1119,13 +3150,6 @@ def all( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) bool True True False - - See Also - -------- - numpy.all - Dataset.all - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.array_all, @@ -1136,20 +3160,20 @@ def all( ) def any( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``any`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -1157,6 +3181,7 @@ def any( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``any`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1164,6 +3189,14 @@ def any( New Dataset with ``any`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.any + dask.array.any + Dataset.any + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -1185,19 +3218,12 @@ def any( da (time) bool True True True True True False >>> ds.resample(time="3M").any() - - Dimensions: (time: 3) - Coordinates: - * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - Data variables: - da (time) bool True True True - - See Also - -------- - numpy.any - Dataset.any - :ref:`resampling` - User guide on resampling operations. + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) bool True True True """ return self.reduce( duck_array_ops.array_any, @@ -1208,25 +3234,25 @@ def any( ) def max( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``max`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -1235,6 +3261,7 @@ def max( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``max`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1242,6 +3269,14 @@ def max( New Dataset with ``max`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.max + dask.array.max + Dataset.max + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -1279,13 +3314,6 @@ def max( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) float64 1.0 3.0 nan - - See Also - -------- - numpy.max - Dataset.max - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.max, @@ -1297,25 +3325,25 @@ def max( ) def min( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``min`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -1324,6 +3352,7 @@ def min( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``min`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1331,6 +3360,14 @@ def min( New Dataset with ``min`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.min + dask.array.min + Dataset.min + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -1368,13 +3405,6 @@ def min( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) float64 1.0 1.0 nan - - See Also - -------- - numpy.min - Dataset.min - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.min, @@ -1386,25 +3416,25 @@ def min( ) def mean( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``mean`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -1413,6 +3443,7 @@ def mean( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``mean`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1420,6 +3451,18 @@ def mean( New Dataset with ``mean`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.mean + dask.array.mean + Dataset.mean + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -1457,13 +3500,6 @@ def mean( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) float64 1.0 2.0 nan - - See Also - -------- - numpy.mean - Dataset.mean - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.mean, @@ -1475,26 +3511,26 @@ def mean( ) def prod( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``prod`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -1509,6 +3545,7 @@ def prod( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``prod`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1516,6 +3553,18 @@ def prod( New Dataset with ``prod`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.prod + dask.array.prod + Dataset.prod + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -1563,13 +3612,6 @@ def prod( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) float64 nan 6.0 nan - - See Also - -------- - numpy.prod - Dataset.prod - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.prod, @@ -1582,26 +3624,26 @@ def prod( ) def sum( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``sum`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -1616,6 +3658,7 @@ def sum( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``sum`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1623,6 +3666,18 @@ def sum( New Dataset with ``sum`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.sum + dask.array.sum + Dataset.sum + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -1670,13 +3725,6 @@ def sum( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) float64 nan 6.0 nan - - See Also - -------- - numpy.sum - Dataset.sum - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.sum, @@ -1689,26 +3737,30 @@ def sum( ) def std( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, + ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``std`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -1716,6 +3768,7 @@ def std( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``std`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1723,6 +3776,18 @@ def std( New Dataset with ``std`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.std + dask.array.std + Dataset.std + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -1761,43 +3826,51 @@ def std( Data variables: da (time) float64 0.0 0.8165 nan - See Also - -------- - numpy.std - Dataset.std - :ref:`resampling` - User guide on resampling operations. + Specify ``ddof=1`` for an unbiased estimate. + + >>> ds.resample(time="3M").std(skipna=True, ddof=1) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 nan 1.0 nan """ return self.reduce( duck_array_ops.std, dim=dim, skipna=skipna, + ddof=ddof, numeric_only=True, keep_attrs=keep_attrs, **kwargs, ) def var( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, + ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``var`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -1805,6 +3878,7 @@ def var( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``var`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1812,6 +3886,18 @@ def var( New Dataset with ``var`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.var + dask.array.var + Dataset.var + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -1850,42 +3936,46 @@ def var( Data variables: da (time) float64 0.0 0.6667 nan - See Also - -------- - numpy.var - Dataset.var - :ref:`resampling` - User guide on resampling operations. + Specify ``ddof=1`` for an unbiased estimate. + + >>> ds.resample(time="3M").var(skipna=True, ddof=1) + + Dimensions: (time: 3) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 + Data variables: + da (time) float64 nan 1.0 nan """ return self.reduce( duck_array_ops.var, dim=dim, skipna=skipna, + ddof=ddof, numeric_only=True, keep_attrs=keep_attrs, **kwargs, ) def median( - self: DatasetReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_Dataset: + ) -> "Dataset": """ Reduce this Dataset's data by applying ``median`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -1894,6 +3984,7 @@ def median( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``median`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -1901,6 +3992,18 @@ def median( New Dataset with ``median`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.median + dask.array.median + Dataset.median + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -1938,13 +4041,6 @@ def median( * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 Data variables: da (time) float64 1.0 2.0 nan - - See Also - -------- - numpy.median - Dataset.median - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.median, @@ -1956,37 +4052,36 @@ def median( ) -class DataArrayReduce(Protocol): +class DataArrayGroupByReductions: + __slots__ = () + def reduce( self, func: Callable[..., Any], dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, axis: Union[None, int, Sequence[int]] = None, keep_attrs: bool = None, keepdims: bool = False, **kwargs: Any, - ) -> T_DataArray: - ... - - -class DataArrayGroupByReductions: - __slots__ = () + ) -> "DataArray": + raise NotImplementedError() def count( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``count`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -1994,6 +4089,7 @@ def count( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``count`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2001,6 +4097,14 @@ def count( New DataArray with ``count`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.count + dask.array.count + DataArray.count + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -2023,13 +4127,6 @@ def count( array([1, 2, 2]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - See Also - -------- - numpy.count - DataArray.count - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.count, @@ -2039,20 +4136,20 @@ def count( ) def all( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``all`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -2060,6 +4157,7 @@ def all( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``all`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2067,6 +4165,14 @@ def all( New DataArray with ``all`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.all + dask.array.all + DataArray.all + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -2089,13 +4195,6 @@ def all( array([False, True, True]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - See Also - -------- - numpy.all - DataArray.all - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.array_all, @@ -2105,20 +4204,20 @@ def all( ) def any( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``any`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -2126,6 +4225,7 @@ def any( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``any`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2133,6 +4233,14 @@ def any( New DataArray with ``any`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.any + dask.array.any + DataArray.any + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -2155,13 +4263,6 @@ def any( array([ True, True, True]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - See Also - -------- - numpy.any - DataArray.any - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.array_any, @@ -2171,25 +4272,25 @@ def any( ) def max( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``max`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -2198,6 +4299,7 @@ def max( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``max`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2205,6 +4307,14 @@ def max( New DataArray with ``max`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.max + dask.array.max + DataArray.max + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -2235,13 +4345,6 @@ def max( array([nan, 2., 3.]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - See Also - -------- - numpy.max - DataArray.max - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.max, @@ -2252,25 +4355,25 @@ def max( ) def min( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``min`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -2279,6 +4382,7 @@ def min( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``min`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2286,6 +4390,14 @@ def min( New DataArray with ``min`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.min + dask.array.min + DataArray.min + :ref:`groupby` + User guide on groupby operations. + Examples -------- >>> da = xr.DataArray( @@ -2316,13 +4428,6 @@ def min( array([nan, 2., 1.]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - See Also - -------- - numpy.min - DataArray.min - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.min, @@ -2333,25 +4438,25 @@ def min( ) def mean( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``mean`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -2360,6 +4465,7 @@ def mean( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``mean`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2367,6 +4473,18 @@ def mean( New DataArray with ``mean`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.mean + dask.array.mean + DataArray.mean + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -2397,13 +4515,6 @@ def mean( array([nan, 2., 2.]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - See Also - -------- - numpy.mean - DataArray.mean - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.mean, @@ -2414,26 +4525,26 @@ def mean( ) def prod( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``prod`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -2448,6 +4559,7 @@ def prod( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``prod`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2455,6 +4567,18 @@ def prod( New DataArray with ``prod`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.prod + dask.array.prod + DataArray.prod + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -2493,13 +4617,6 @@ def prod( array([nan, 4., 3.]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - See Also - -------- - numpy.prod - DataArray.prod - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.prod, @@ -2511,26 +4628,26 @@ def prod( ) def sum( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``sum`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -2545,6 +4662,7 @@ def sum( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``sum`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2552,6 +4670,18 @@ def sum( New DataArray with ``sum`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.sum + dask.array.sum + DataArray.sum + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -2590,13 +4720,6 @@ def sum( array([nan, 4., 4.]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - See Also - -------- - numpy.sum - DataArray.sum - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.sum, @@ -2608,26 +4731,30 @@ def sum( ) def std( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, + ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``std`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -2635,6 +4762,7 @@ def std( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``std`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2642,6 +4770,18 @@ def std( New DataArray with ``std`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.std + dask.array.std + DataArray.std + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -2673,42 +4813,48 @@ def std( Coordinates: * labels (labels) object 'a' 'b' 'c' - See Also - -------- - numpy.std - DataArray.std - :ref:`groupby` - User guide on groupby operations. + Specify ``ddof=1`` for an unbiased estimate. + + >>> da.groupby("labels").std(skipna=True, ddof=1) + + array([ nan, 0. , 1.41421356]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.std, dim=dim, skipna=skipna, + ddof=ddof, keep_attrs=keep_attrs, **kwargs, ) def var( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, + ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``var`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -2716,6 +4862,7 @@ def var( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``var`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2723,6 +4870,18 @@ def var( New DataArray with ``var`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.var + dask.array.var + DataArray.var + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -2754,41 +4913,43 @@ def var( Coordinates: * labels (labels) object 'a' 'b' 'c' - See Also - -------- - numpy.var - DataArray.var - :ref:`groupby` - User guide on groupby operations. + Specify ``ddof=1`` for an unbiased estimate. + + >>> da.groupby("labels").var(skipna=True, ddof=1) + + array([nan, 0., 2.]) + Coordinates: + * labels (labels) object 'a' 'b' 'c' """ return self.reduce( duck_array_ops.var, dim=dim, skipna=skipna, + ddof=ddof, keep_attrs=keep_attrs, **kwargs, ) def median( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``median`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -2797,6 +4958,7 @@ def median( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``median`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2804,6 +4966,18 @@ def median( New DataArray with ``median`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.median + dask.array.median + DataArray.median + :ref:`groupby` + User guide on groupby operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -2834,13 +5008,6 @@ def median( array([nan, 2., 2.]) Coordinates: * labels (labels) object 'a' 'b' 'c' - - See Also - -------- - numpy.median - DataArray.median - :ref:`groupby` - User guide on groupby operations. """ return self.reduce( duck_array_ops.median, @@ -2854,21 +5021,33 @@ def median( class DataArrayResampleReductions: __slots__ = () + def reduce( + self, + func: Callable[..., Any], + dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, + axis: Union[None, int, Sequence[int]] = None, + keep_attrs: bool = None, + keepdims: bool = False, + **kwargs: Any, + ) -> "DataArray": + raise NotImplementedError() + def count( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``count`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -2876,6 +5055,7 @@ def count( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``count`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2883,6 +5063,14 @@ def count( New DataArray with ``count`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.count + dask.array.count + DataArray.count + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -2905,13 +5093,6 @@ def count( array([1, 3, 1]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - See Also - -------- - numpy.count - DataArray.count - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.count, @@ -2921,20 +5102,20 @@ def count( ) def all( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``all`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -2942,6 +5123,7 @@ def all( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``all`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -2949,6 +5131,14 @@ def all( New DataArray with ``all`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.all + dask.array.all + DataArray.all + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -2971,13 +5161,6 @@ def all( array([ True, True, False]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - See Also - -------- - numpy.all - DataArray.all - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.array_all, @@ -2987,20 +5170,20 @@ def all( ) def any( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, + *, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``any`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -3008,6 +5191,7 @@ def any( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``any`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3015,6 +5199,14 @@ def any( New DataArray with ``any`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.any + dask.array.any + DataArray.any + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -3037,13 +5229,6 @@ def any( array([ True, True, True]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - See Also - -------- - numpy.any - DataArray.any - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.array_any, @@ -3053,25 +5238,25 @@ def any( ) def max( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``max`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``max``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -3080,6 +5265,7 @@ def max( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``max`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3087,6 +5273,14 @@ def max( New DataArray with ``max`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.max + dask.array.max + DataArray.max + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -3117,13 +5311,6 @@ def max( array([ 1., 3., nan]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - See Also - -------- - numpy.max - DataArray.max - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.max, @@ -3134,25 +5321,25 @@ def max( ) def min( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``min`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``min``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -3161,6 +5348,7 @@ def min( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``min`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3168,6 +5356,14 @@ def min( New DataArray with ``min`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.min + dask.array.min + DataArray.min + :ref:`resampling` + User guide on resampling operations. + Examples -------- >>> da = xr.DataArray( @@ -3198,13 +5394,6 @@ def min( array([ 1., 1., nan]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - See Also - -------- - numpy.min - DataArray.min - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.min, @@ -3215,25 +5404,25 @@ def min( ) def mean( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``mean`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``mean``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -3242,6 +5431,7 @@ def mean( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``mean`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3249,6 +5439,18 @@ def mean( New DataArray with ``mean`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.mean + dask.array.mean + DataArray.mean + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -3279,13 +5481,6 @@ def mean( array([ 1., 2., nan]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - See Also - -------- - numpy.mean - DataArray.mean - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.mean, @@ -3296,26 +5491,26 @@ def mean( ) def prod( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``prod`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``prod``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -3330,6 +5525,7 @@ def prod( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``prod`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3337,6 +5533,18 @@ def prod( New DataArray with ``prod`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.prod + dask.array.prod + DataArray.prod + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -3375,13 +5583,6 @@ def prod( array([nan, 6., nan]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - See Also - -------- - numpy.prod - DataArray.prod - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.prod, @@ -3393,26 +5594,26 @@ def prod( ) def sum( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, min_count: Optional[int] = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``sum`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``sum``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). min_count : int, default: None The required number of valid values to perform the operation. If @@ -3427,6 +5628,7 @@ def sum( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``sum`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3434,6 +5636,18 @@ def sum( New DataArray with ``sum`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.sum + dask.array.sum + DataArray.sum + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -3472,13 +5686,6 @@ def sum( array([nan, 6., nan]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - See Also - -------- - numpy.sum - DataArray.sum - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.sum, @@ -3490,26 +5697,30 @@ def sum( ) def std( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, + ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``std`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``std``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -3517,6 +5728,7 @@ def std( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``std`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3524,6 +5736,18 @@ def std( New DataArray with ``std`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.std + dask.array.std + DataArray.std + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -3555,42 +5779,48 @@ def std( Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - See Also - -------- - numpy.std - DataArray.std - :ref:`resampling` - User guide on resampling operations. + Specify ``ddof=1`` for an unbiased estimate. + + >>> da.resample(time="3M").std(skipna=True, ddof=1) + + array([nan, 1., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.std, dim=dim, skipna=skipna, + ddof=ddof, keep_attrs=keep_attrs, **kwargs, ) def var( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, + ddof: int = 0, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``var`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``var``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). + ddof : int, default: 0 + “Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``, + where ``N`` represents the number of elements. keep_attrs : bool, optional If True, ``attrs`` will be copied from the original object to the new one. If False (default), the new object will be @@ -3598,6 +5828,7 @@ def var( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``var`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3605,6 +5836,18 @@ def var( New DataArray with ``var`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.var + dask.array.var + DataArray.var + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -3636,41 +5879,43 @@ def var( Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - See Also - -------- - numpy.var - DataArray.var - :ref:`resampling` - User guide on resampling operations. + Specify ``ddof=1`` for an unbiased estimate. + + >>> da.resample(time="3M").var(skipna=True, ddof=1) + + array([nan, 1., nan]) + Coordinates: + * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 """ return self.reduce( duck_array_ops.var, dim=dim, skipna=skipna, + ddof=ddof, keep_attrs=keep_attrs, **kwargs, ) def median( - self: DataArrayReduce, + self, dim: Union[None, Hashable, Sequence[Hashable]] = None, - skipna: bool = True, + *, + skipna: bool = None, keep_attrs: bool = None, **kwargs, - ) -> T_DataArray: + ) -> "DataArray": """ Reduce this DataArray's data by applying ``median`` along some dimension(s). Parameters ---------- - dim : hashable or iterable of hashable, optional + dim : hashable or iterable of hashable, default: None Name of dimension[s] along which to apply ``median``. For e.g. ``dim="x"`` - or ``dim=["x", "y"]``. If ``None``, will reduce over all dimensions - present in the grouped variable. - skipna : bool, optional + or ``dim=["x", "y"]``. If None, will reduce over all dimensions. + skipna : bool, default: None If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been + have a sentinel missing value (int) or ``skipna=True`` has not been implemented (object, datetime64 or timedelta64). keep_attrs : bool, optional If True, ``attrs`` will be copied from the original @@ -3679,6 +5924,7 @@ def median( **kwargs : dict Additional keyword arguments passed on to the appropriate array function for calculating ``median`` on this object's data. + These could include dask-specific kwargs like ``split_every``. Returns ------- @@ -3686,6 +5932,18 @@ def median( New DataArray with ``median`` applied to its data and the indicated dimension(s) removed + See Also + -------- + numpy.median + dask.array.median + DataArray.median + :ref:`resampling` + User guide on resampling operations. + + Notes + ----- + Non-numeric variables will be removed prior to reducing. + Examples -------- >>> da = xr.DataArray( @@ -3716,13 +5974,6 @@ def median( array([ 1., 2., nan]) Coordinates: * time (time) datetime64[ns] 2001-01-31 2001-04-30 2001-07-31 - - See Also - -------- - numpy.median - DataArray.median - :ref:`resampling` - User guide on resampling operations. """ return self.reduce( duck_array_ops.median, diff --git a/xarray/util/generate_reductions.py b/xarray/util/generate_reductions.py index 2e5d8348c85..e79c94e8907 100644 --- a/xarray/util/generate_reductions.py +++ b/xarray/util/generate_reductions.py @@ -30,6 +30,7 @@ CLASS_PREAMBLE = """ + class {obj}{cls}Reductions: __slots__ = () From d2a883cfc18300653862ca75766fb4ea2a665be7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Mar 2022 00:50:55 +0000 Subject: [PATCH 31/31] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/tests/test_duck_array_ops.py | 1 - 1 file changed, 1 deletion(-) diff --git a/xarray/tests/test_duck_array_ops.py b/xarray/tests/test_duck_array_ops.py index 93442975b6d..c329bc50c56 100644 --- a/xarray/tests/test_duck_array_ops.py +++ b/xarray/tests/test_duck_array_ops.py @@ -1,6 +1,5 @@ import datetime as dt import warnings -from textwrap import dedent import numpy as np import pandas as pd