Skip to content

Commit

Permalink
min_periods defaults to 1
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Dec 8, 2023
1 parent 0ab3806 commit 500d11f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
6 changes: 3 additions & 3 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -6935,7 +6935,7 @@ def rolling(
def cumulative(
self,
dim: str | Iterable[Hashable],
min_periods: int | None = 1,
min_periods: int = 1,
) -> DataArrayRolling:
"""
Accumulating object for DataArrays.
Expand All @@ -6944,7 +6944,7 @@ def cumulative(
----------
dims : iterable of hashable
The name(s) of the dimensions to create the cumulative window along
min_periods : int or None, default: None
min_periods : int, default: 1
Minimum number of observations in window required to have a value
(otherwise result is NA). The default is 1 (note this is different
from ``Rolling``, whose default is the size of the window).
Expand Down Expand Up @@ -7005,7 +7005,7 @@ def cumulative(
)
dim = {d: self.sizes[d] for d in dim}

return DataArrayRolling(self, dim, min_periods=min_periods or 1, center=False)
return DataArrayRolling(self, dim, min_periods=min_periods, center=False)

def coarsen(
self,
Expand Down
6 changes: 3 additions & 3 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -10381,7 +10381,7 @@ def rolling(
def cumulative(
self,
dim: str | Iterable[Hashable],
min_periods: int | None = 1,
min_periods: int = 1,
) -> DatasetRolling:
"""
Accumulating object for Datasets
Expand All @@ -10390,7 +10390,7 @@ def cumulative(
----------
dims : iterable of hashable
The name(s) of the dimensions to create the cumulative window along
min_periods : int or None, default: None
min_periods : int, default: 1
Minimum number of observations in window required to have a value
(otherwise result is NA). The default is 1 (note this is different
from ``Rolling``, whose default is the size of the window).
Expand Down Expand Up @@ -10421,7 +10421,7 @@ def cumulative(
)
dim = {d: self.sizes[d] for d in dim}

return DatasetRolling(self, dim, min_periods=min_periods or 1, center=False)
return DatasetRolling(self, dim, min_periods=min_periods, center=False)

def coarsen(
self,
Expand Down
17 changes: 11 additions & 6 deletions xarray/tests/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,23 +486,28 @@ def test_rolling_exp_keep_attrs(self, da, func) -> None:
da.rolling_exp(time=10, keep_attrs=True)

@pytest.mark.parametrize("func", ["mean", "sum"])
@pytest.mark.parametrize("min_periods", [None, 1, 20])
@pytest.mark.parametrize("min_periods", [1, 20])
def test_cumulative(self, da, func, min_periods) -> None:
# One dim
result = getattr(da.cumulative("time", min_periods=min_periods), func)()
expected = getattr(
da.rolling(time=da.time.size, min_periods=min_periods or 1), func
da.rolling(time=da.time.size, min_periods=min_periods), func
)()
assert_identical(result, expected)

# Multiple dim
result = getattr(da.cumulative(["time", "a"], min_periods=min_periods), func)()
expected = getattr(
da.rolling(time=da.time.size, a=da.a.size, min_periods=min_periods or 1),
da.rolling(time=da.time.size, a=da.a.size, min_periods=min_periods),
func,
)()
assert_identical(result, expected)

def test_cumulative_vs_cum(self, da) -> None:
result = da.cumulative("time").sum()
expected = da.cumsum("time")
assert_identical(result, expected)


class TestDatasetRolling:
@pytest.mark.parametrize(
Expand Down Expand Up @@ -829,19 +834,19 @@ def test_raise_no_warning_dask_rolling_assert_close(self, ds, name) -> None:

@pytest.mark.parametrize("func", ["mean", "sum"])
@pytest.mark.parametrize("ds", (2,), indirect=True)
@pytest.mark.parametrize("min_periods", [None, 1, 10])
@pytest.mark.parametrize("min_periods", [1, 10])
def test_cumulative(self, ds, func, min_periods) -> None:
# One dim
result = getattr(ds.cumulative("time", min_periods=min_periods), func)()
expected = getattr(
ds.rolling(time=ds.time.size, min_periods=min_periods or 1), func
ds.rolling(time=ds.time.size, min_periods=min_periods), func
)()
assert_identical(result, expected)

# Multiple dim
result = getattr(ds.cumulative(["time", "x"], min_periods=min_periods), func)()
expected = getattr(
ds.rolling(time=ds.time.size, x=ds.x.size, min_periods=min_periods or 1),
ds.rolling(time=ds.time.size, x=ds.x.size, min_periods=min_periods),
func,
)()
assert_identical(result, expected)
Expand Down

0 comments on commit 500d11f

Please sign in to comment.