Skip to content

Commit 500d11f

Browse files
committed
min_periods defaults to 1
1 parent 0ab3806 commit 500d11f

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

xarray/core/dataarray.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6935,7 +6935,7 @@ def rolling(
69356935
def cumulative(
69366936
self,
69376937
dim: str | Iterable[Hashable],
6938-
min_periods: int | None = 1,
6938+
min_periods: int = 1,
69396939
) -> DataArrayRolling:
69406940
"""
69416941
Accumulating object for DataArrays.
@@ -6944,7 +6944,7 @@ def cumulative(
69446944
----------
69456945
dims : iterable of hashable
69466946
The name(s) of the dimensions to create the cumulative window along
6947-
min_periods : int or None, default: None
6947+
min_periods : int, default: 1
69486948
Minimum number of observations in window required to have a value
69496949
(otherwise result is NA). The default is 1 (note this is different
69506950
from ``Rolling``, whose default is the size of the window).
@@ -7005,7 +7005,7 @@ def cumulative(
70057005
)
70067006
dim = {d: self.sizes[d] for d in dim}
70077007

7008-
return DataArrayRolling(self, dim, min_periods=min_periods or 1, center=False)
7008+
return DataArrayRolling(self, dim, min_periods=min_periods, center=False)
70097009

70107010
def coarsen(
70117011
self,

xarray/core/dataset.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10381,7 +10381,7 @@ def rolling(
1038110381
def cumulative(
1038210382
self,
1038310383
dim: str | Iterable[Hashable],
10384-
min_periods: int | None = 1,
10384+
min_periods: int = 1,
1038510385
) -> DatasetRolling:
1038610386
"""
1038710387
Accumulating object for Datasets
@@ -10390,7 +10390,7 @@ def cumulative(
1039010390
----------
1039110391
dims : iterable of hashable
1039210392
The name(s) of the dimensions to create the cumulative window along
10393-
min_periods : int or None, default: None
10393+
min_periods : int, default: 1
1039410394
Minimum number of observations in window required to have a value
1039510395
(otherwise result is NA). The default is 1 (note this is different
1039610396
from ``Rolling``, whose default is the size of the window).
@@ -10421,7 +10421,7 @@ def cumulative(
1042110421
)
1042210422
dim = {d: self.sizes[d] for d in dim}
1042310423

10424-
return DatasetRolling(self, dim, min_periods=min_periods or 1, center=False)
10424+
return DatasetRolling(self, dim, min_periods=min_periods, center=False)
1042510425

1042610426
def coarsen(
1042710427
self,

xarray/tests/test_rolling.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,23 +486,28 @@ def test_rolling_exp_keep_attrs(self, da, func) -> None:
486486
da.rolling_exp(time=10, keep_attrs=True)
487487

488488
@pytest.mark.parametrize("func", ["mean", "sum"])
489-
@pytest.mark.parametrize("min_periods", [None, 1, 20])
489+
@pytest.mark.parametrize("min_periods", [1, 20])
490490
def test_cumulative(self, da, func, min_periods) -> None:
491491
# One dim
492492
result = getattr(da.cumulative("time", min_periods=min_periods), func)()
493493
expected = getattr(
494-
da.rolling(time=da.time.size, min_periods=min_periods or 1), func
494+
da.rolling(time=da.time.size, min_periods=min_periods), func
495495
)()
496496
assert_identical(result, expected)
497497

498498
# Multiple dim
499499
result = getattr(da.cumulative(["time", "a"], min_periods=min_periods), func)()
500500
expected = getattr(
501-
da.rolling(time=da.time.size, a=da.a.size, min_periods=min_periods or 1),
501+
da.rolling(time=da.time.size, a=da.a.size, min_periods=min_periods),
502502
func,
503503
)()
504504
assert_identical(result, expected)
505505

506+
def test_cumulative_vs_cum(self, da) -> None:
507+
result = da.cumulative("time").sum()
508+
expected = da.cumsum("time")
509+
assert_identical(result, expected)
510+
506511

507512
class TestDatasetRolling:
508513
@pytest.mark.parametrize(
@@ -829,19 +834,19 @@ def test_raise_no_warning_dask_rolling_assert_close(self, ds, name) -> None:
829834

830835
@pytest.mark.parametrize("func", ["mean", "sum"])
831836
@pytest.mark.parametrize("ds", (2,), indirect=True)
832-
@pytest.mark.parametrize("min_periods", [None, 1, 10])
837+
@pytest.mark.parametrize("min_periods", [1, 10])
833838
def test_cumulative(self, ds, func, min_periods) -> None:
834839
# One dim
835840
result = getattr(ds.cumulative("time", min_periods=min_periods), func)()
836841
expected = getattr(
837-
ds.rolling(time=ds.time.size, min_periods=min_periods or 1), func
842+
ds.rolling(time=ds.time.size, min_periods=min_periods), func
838843
)()
839844
assert_identical(result, expected)
840845

841846
# Multiple dim
842847
result = getattr(ds.cumulative(["time", "x"], min_periods=min_periods), func)()
843848
expected = getattr(
844-
ds.rolling(time=ds.time.size, x=ds.x.size, min_periods=min_periods or 1),
849+
ds.rolling(time=ds.time.size, x=ds.x.size, min_periods=min_periods),
845850
func,
846851
)()
847852
assert_identical(result, expected)

0 commit comments

Comments
 (0)