@@ -836,7 +836,9 @@ class DataArrayCoarsen(Coarsen):
836
836
_reduce_extra_args_docstring = """"""
837
837
838
838
@classmethod
839
- def _reduce_method (cls , func : Callable , include_skipna : bool , numeric_only : bool ):
839
+ def _reduce_method (
840
+ cls , func : Callable , include_skipna : bool = False , numeric_only : bool = False
841
+ ):
840
842
"""
841
843
Return a wrapped function for injecting reduction methods.
842
844
see ops.inject_reduce_methods
@@ -871,14 +873,48 @@ def wrapped_func(self, **kwargs):
871
873
872
874
return wrapped_func
873
875
876
+ def reduce (self , func : Callable , ** kwargs ):
877
+ """Reduce the items in this group by applying `func` along some
878
+ dimension(s).
879
+
880
+ Parameters
881
+ ----------
882
+ func : callable
883
+ Function which can be called in the form `func(x, axis, **kwargs)`
884
+ to return the result of collapsing an np.ndarray over the coarsening
885
+ dimensions. It must be possible to provide the `axis` argument
886
+ with a tuple of integers.
887
+ **kwargs : dict
888
+ Additional keyword arguments passed on to `func`.
889
+
890
+ Returns
891
+ -------
892
+ reduced : DataArray
893
+ Array with summarized data.
894
+
895
+ Examples
896
+ --------
897
+ >>> da = xr.DataArray(np.arange(8).reshape(2, 4), dims=("a", "b"))
898
+ >>> coarsen = da.coarsen(b=2)
899
+ >>> coarsen.reduce(np.sum)
900
+ <xarray.DataArray (a: 2, b: 2)>
901
+ array([[ 1, 5],
902
+ [ 9, 13]])
903
+ Dimensions without coordinates: a, b
904
+ """
905
+ wrapped_func = self ._reduce_method (func )
906
+ return wrapped_func (self , ** kwargs )
907
+
874
908
875
909
class DatasetCoarsen (Coarsen ):
876
910
__slots__ = ()
877
911
878
912
_reduce_extra_args_docstring = """"""
879
913
880
914
@classmethod
881
- def _reduce_method (cls , func : Callable , include_skipna : bool , numeric_only : bool ):
915
+ def _reduce_method (
916
+ cls , func : Callable , include_skipna : bool = False , numeric_only : bool = False
917
+ ):
882
918
"""
883
919
Return a wrapped function for injecting reduction methods.
884
920
see ops.inject_reduce_methods
@@ -917,6 +953,28 @@ def wrapped_func(self, **kwargs):
917
953
918
954
return wrapped_func
919
955
956
+ def reduce (self , func : Callable , ** kwargs ):
957
+ """Reduce the items in this group by applying `func` along some
958
+ dimension(s).
959
+
960
+ Parameters
961
+ ----------
962
+ func : callable
963
+ Function which can be called in the form `func(x, axis, **kwargs)`
964
+ to return the result of collapsing an np.ndarray over the coarsening
965
+ dimensions. It must be possible to provide the `axis` argument with
966
+ a tuple of integers.
967
+ **kwargs : dict
968
+ Additional keyword arguments passed on to `func`.
969
+
970
+ Returns
971
+ -------
972
+ reduced : Dataset
973
+ Arrays with summarized data.
974
+ """
975
+ wrapped_func = self ._reduce_method (func )
976
+ return wrapped_func (self , ** kwargs )
977
+
920
978
921
979
inject_reduce_methods (DataArrayCoarsen )
922
980
inject_reduce_methods (DatasetCoarsen )
0 commit comments