Skip to content
forked from pydata/xarray

Commit

Permalink
Add cumsum to DatasetGroupBy
Browse files Browse the repository at this point in the history
  • Loading branch information
VladSkripniuk authored and dcherian committed Apr 27, 2022
1 parent 33cdabd commit a7811cd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .concat import concat
from .formatting import format_array_flat
from .indexes import create_default_index_implicit, filter_indexes_from_coords
from .ops import IncludeCumMethods
from .options import _get_keep_attrs
from .pycompat import integer_types
from .utils import (
Expand Down Expand Up @@ -970,7 +971,7 @@ def reduce_array(ar):
return self.map(reduce_array, shortcut=shortcut)


class DataArrayGroupBy(DataArrayGroupByBase, DataArrayGroupByReductions):
class DataArrayGroupBy(DataArrayGroupByBase, DataArrayGroupByReductions, IncludeCumMethods):
__slots__ = ()


Expand Down Expand Up @@ -1108,5 +1109,5 @@ def assign(self, **kwargs):
return self.map(lambda ds: ds.assign(**kwargs))


class DatasetGroupBy(DatasetGroupByBase, DatasetGroupByReductions):
class DatasetGroupBy(DatasetGroupByBase, DatasetGroupByReductions, IncludeCumMethods):
__slots__ = ()
19 changes: 19 additions & 0 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1918,4 +1918,23 @@ def func(arg1, arg2, arg3=0.0):
assert_identical(expected, actual)


def test_groupby_cumsum():
ds = xr.Dataset(
{"foo": (("x",), [7, 3, 1, 1, 1, 1, 1])},
coords={"x": [0, 1, 2, 3, 4, 5, 6], "group_id": ("x", [0, 0, 1, 1, 2, 2, 2])},
)
actual = ds.groupby("group_id").cumsum(dim="x")
expected = xr.Dataset(
{
"foo": (("x",), [7, 10, 1, 2, 1, 2, 3]),
"group_id": (("x",), [0, 0, 1, 1, 2, 2, 2]),
},
coords={"x": [0, 1, 2, 3, 4, 5, 6]},
)
assert_identical(expected, actual)

actual = ds.foo.groupby(ds.group_id).cumsum(dim="x")
assert_identical(expected.foo, actual)


# TODO: move other groupby tests from test_dataset and test_dataarray over here

0 comments on commit a7811cd

Please sign in to comment.