@@ -6923,14 +6923,90 @@ def rolling(
6923
6923
6924
6924
See Also
6925
6925
--------
6926
- core.rolling.DataArrayRolling
6926
+ DataArray.cumulative
6927
6927
Dataset.rolling
6928
+ core.rolling.DataArrayRolling
6928
6929
"""
6929
6930
from xarray .core .rolling import DataArrayRolling
6930
6931
6931
6932
dim = either_dict_or_kwargs (dim , window_kwargs , "rolling" )
6932
6933
return DataArrayRolling (self , dim , min_periods = min_periods , center = center )
6933
6934
6935
+ def cumulative (
6936
+ self ,
6937
+ dim : str | Iterable [Hashable ],
6938
+ min_periods : int = 1 ,
6939
+ ) -> DataArrayRolling :
6940
+ """
6941
+ Accumulating object for DataArrays.
6942
+
6943
+ Parameters
6944
+ ----------
6945
+ dims : iterable of hashable
6946
+ The name(s) of the dimensions to create the cumulative window along
6947
+ min_periods : int, default: 1
6948
+ Minimum number of observations in window required to have a value
6949
+ (otherwise result is NA). The default is 1 (note this is different
6950
+ from ``Rolling``, whose default is the size of the window).
6951
+
6952
+ Returns
6953
+ -------
6954
+ core.rolling.DataArrayRolling
6955
+
6956
+ Examples
6957
+ --------
6958
+ Create rolling seasonal average of monthly data e.g. DJF, JFM, ..., SON:
6959
+
6960
+ >>> da = xr.DataArray(
6961
+ ... np.linspace(0, 11, num=12),
6962
+ ... coords=[
6963
+ ... pd.date_range(
6964
+ ... "1999-12-15",
6965
+ ... periods=12,
6966
+ ... freq=pd.DateOffset(months=1),
6967
+ ... )
6968
+ ... ],
6969
+ ... dims="time",
6970
+ ... )
6971
+
6972
+ >>> da
6973
+ <xarray.DataArray (time: 12)>
6974
+ array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.])
6975
+ Coordinates:
6976
+ * time (time) datetime64[ns] 1999-12-15 2000-01-15 ... 2000-11-15
6977
+
6978
+ >>> da.cumulative("time").sum()
6979
+ <xarray.DataArray (time: 12)>
6980
+ array([ 0., 1., 3., 6., 10., 15., 21., 28., 36., 45., 55., 66.])
6981
+ Coordinates:
6982
+ * time (time) datetime64[ns] 1999-12-15 2000-01-15 ... 2000-11-15
6983
+
6984
+ See Also
6985
+ --------
6986
+ DataArray.rolling
6987
+ Dataset.cumulative
6988
+ core.rolling.DataArrayRolling
6989
+ """
6990
+ from xarray .core .rolling import DataArrayRolling
6991
+
6992
+ # Could we abstract this "normalize and check 'dim'" logic? It's currently shared
6993
+ # with the same method in Dataset.
6994
+ if isinstance (dim , str ):
6995
+ if dim not in self .dims :
6996
+ raise ValueError (
6997
+ f"Dimension { dim } not found in data dimensions: { self .dims } "
6998
+ )
6999
+ dim = {dim : self .sizes [dim ]}
7000
+ else :
7001
+ missing_dims = set (dim ) - set (self .dims )
7002
+ if missing_dims :
7003
+ raise ValueError (
7004
+ f"Dimensions { missing_dims } not found in data dimensions: { self .dims } "
7005
+ )
7006
+ dim = {d : self .sizes [d ] for d in dim }
7007
+
7008
+ return DataArrayRolling (self , dim , min_periods = min_periods , center = False )
7009
+
6934
7010
def coarsen (
6935
7011
self ,
6936
7012
dim : Mapping [Any , int ] | None = None ,
0 commit comments