Skip to content

Commit dabd977

Browse files
dcherianVladSkripniukpre-commit-ci[bot]max-sixtyIllviljan
authored
Add cumsum to DatasetGroupBy (#6525)
* Add cumsum to DatasetGroupBy Fixes #3141 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix * More fix. * Add whats-new * [skip-ci] Add to api.rst * Update xarray/tests/test_groupby.py Co-authored-by: Illviljan <[email protected]> * Update xarray/core/groupby.py * Update xarray/core/groupby.py * Update xarray/tests/test_groupby.py Co-authored-by: Vlad Skripniuk <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maximilian Roos <[email protected]> Co-authored-by: Illviljan <[email protected]> Co-authored-by: Anderson Banihirwe <[email protected]>
1 parent 9b54b44 commit dabd977

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

doc/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ Dataset
736736
DatasetGroupBy.all
737737
DatasetGroupBy.any
738738
DatasetGroupBy.count
739+
DatasetGroupBy.cumsum
739740
DatasetGroupBy.max
740741
DatasetGroupBy.mean
741742
DatasetGroupBy.median
@@ -765,6 +766,7 @@ DataArray
765766
DataArrayGroupBy.all
766767
DataArrayGroupBy.any
767768
DataArrayGroupBy.count
769+
DataArrayGroupBy.cumsum
768770
DataArrayGroupBy.max
769771
DataArrayGroupBy.mean
770772
DataArrayGroupBy.median

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ New Features
123123
- Allow passing chunks in ``**kwargs`` form to :py:meth:`Dataset.chunk`, :py:meth:`DataArray.chunk`, and
124124
:py:meth:`Variable.chunk`. (:pull:`6471`)
125125
By `Tom Nicholas <https://github.com/TomNicholas>`_.
126+
- Add :py:meth:`core.groupby.DatasetGroupBy.cumsum` and :py:meth:`core.groupby.DataArrayGroupBy.cumsum`.
127+
By `Vladislav Skripniuk <https://github.com/VladSkripniuk>`_ and `Deepak Cherian <https://github.com/dcherian>`_. (:pull:`3147`, :pull:`6525`, :issue:`3141`)
128+
- Expose `inline_array` kwarg from `dask.array.from_array` in :py:func:`open_dataset`, :py:meth:`Dataset.chunk`,
129+
:py:meth:`DataArray.chunk`, and :py:meth:`Variable.chunk`. (:pull:`6471`)
126130
- Expose the ``inline_array`` kwarg from :py:func:`dask.array.from_array` in :py:func:`open_dataset`,
127131
:py:meth:`Dataset.chunk`, :py:meth:`DataArray.chunk`, and :py:meth:`Variable.chunk`. (:pull:`6471`)
128132
By `Tom Nicholas <https://github.com/TomNicholas>`_.

xarray/core/groupby.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
from ._reductions import DataArrayGroupByReductions, DatasetGroupByReductions
2626
from .alignment import align
2727
from .arithmetic import DataArrayGroupbyArithmetic, DatasetGroupbyArithmetic
28+
from .common import ImplementsArrayReduce, ImplementsDatasetReduce
2829
from .concat import concat
2930
from .formatting import format_array_flat
3031
from .indexes import create_default_index_implicit, filter_indexes_from_coords
3132
from .npcompat import QUANTILE_METHODS, ArrayLike
33+
from .ops import IncludeCumMethods
3234
from .options import _get_keep_attrs
3335
from .pycompat import integer_types
3436
from .types import T_Xarray
@@ -1187,7 +1189,12 @@ def reduce_array(ar: DataArray) -> DataArray:
11871189

11881190

11891191
# https://github.com/python/mypy/issues/9031
1190-
class DataArrayGroupBy(DataArrayGroupByBase, DataArrayGroupByReductions): # type: ignore[misc]
1192+
class DataArrayGroupBy( # type: ignore[misc]
1193+
DataArrayGroupByBase,
1194+
DataArrayGroupByReductions,
1195+
ImplementsArrayReduce,
1196+
IncludeCumMethods,
1197+
):
11911198
__slots__ = ()
11921199

11931200

@@ -1341,5 +1348,10 @@ def assign(self, **kwargs: Any) -> Dataset:
13411348

13421349

13431350
# https://github.com/python/mypy/issues/9031
1344-
class DatasetGroupBy(DatasetGroupByBase, DatasetGroupByReductions): # type: ignore[misc]
1351+
class DatasetGroupBy( # type: ignore[misc]
1352+
DatasetGroupByBase,
1353+
DatasetGroupByReductions,
1354+
ImplementsDatasetReduce,
1355+
IncludeCumMethods,
1356+
):
13451357
__slots__ = ()

xarray/tests/test_groupby.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,3 +2002,31 @@ def func(arg1, arg2, arg3=0.0):
20022002
expected = xr.Dataset({"foo": ("time", [3.0, 3.0, 3.0]), "time": times})
20032003
actual = ds.resample(time="D").map(func, args=(1.0,), arg3=1.0)
20042004
assert_identical(expected, actual)
2005+
2006+
2007+
def test_groupby_cumsum() -> None:
2008+
ds = xr.Dataset(
2009+
{"foo": (("x",), [7, 3, 1, 1, 1, 1, 1])},
2010+
coords={"x": [0, 1, 2, 3, 4, 5, 6], "group_id": ("x", [0, 0, 1, 1, 2, 2, 2])},
2011+
)
2012+
actual = ds.groupby("group_id").cumsum(dim="x") # type: ignore[attr-defined] # TODO: move cumsum to generate_reductions.py
2013+
expected = xr.Dataset(
2014+
{
2015+
"foo": (("x",), [7, 10, 1, 2, 1, 2, 3]),
2016+
},
2017+
coords={
2018+
"x": [0, 1, 2, 3, 4, 5, 6],
2019+
"group_id": ds.group_id,
2020+
},
2021+
)
2022+
# TODO: Remove drop_vars when GH6528 is fixed
2023+
# when Dataset.cumsum propagates indexes, and the group variable?
2024+
assert_identical(expected.drop_vars(["x", "group_id"]), actual)
2025+
2026+
actual = ds.foo.groupby("group_id").cumsum(dim="x")
2027+
expected.coords["group_id"] = ds.group_id
2028+
expected.coords["x"] = np.arange(7)
2029+
assert_identical(expected.foo, actual)
2030+
2031+
2032+
# TODO: move other groupby tests from test_dataset and test_dataarray over here

0 commit comments

Comments
 (0)