Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cumulative examples #7152

Merged
merged 44 commits into from
Oct 26, 2022
Merged
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
7e2f12a
Added file to generate docs for cumulatives. generate_cumulatives.py …
patrick-naylor Oct 8, 2022
d3c05eb
Fixed mypy issues
patrick-naylor Oct 8, 2022
ae513d9
Updated cumulatives to fix mypy issues
patrick-naylor Oct 10, 2022
e9ba30e
Merge branch 'main' into cumulative_examples
patrick-naylor Oct 10, 2022
b774ddb
Added keep_attrs to groupby funcs
patrick-naylor Oct 10, 2022
804d960
commited merge
patrick-naylor Oct 10, 2022
7d95f1d
Combined cumulatives and reductions and aggregations and modified dat…
patrick-naylor Oct 14, 2022
330dce8
Merged cumulatives and reductions into aggregations
patrick-naylor Oct 19, 2022
af5573c
Removed test print from dataset.py
patrick-naylor Oct 19, 2022
42c4990
Removed generate_cumulatives and generate_reductions
patrick-naylor Oct 19, 2022
d9e5267
Merge branch 'main' into cumulative_examples
patrick-naylor Oct 19, 2022
4e611cf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2022
441450b
Updated _aggregations with docstring changes
patrick-naylor Oct 19, 2022
2eac348
Merged origin
patrick-naylor Oct 19, 2022
8f4d798
Updated generate_aggregations.py with suggestions from @dcherian. Rem…
patrick-naylor Oct 21, 2022
bb16d01
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 21, 2022
0bfded1
Removed unecessary function from dataset.py
patrick-naylor Oct 21, 2022
adc8e9f
Merge branch 'cumulative_examples' of https://github.com/patrick-nayl…
patrick-naylor Oct 21, 2022
558fbf0
Removed unecessary function from dataset.py
patrick-naylor Oct 21, 2022
35f438a
Merge branch 'main' into cumulative_examples
patrick-naylor Oct 21, 2022
a9e6f4a
Updated api.rst, whats-new.rst and added a cumprod test to test_group…
patrick-naylor Oct 21, 2022
29f3238
Merge branch 'cumulative_examples' of https://github.com/patrick-nayl…
patrick-naylor Oct 21, 2022
9ba655b
Fixed accidental edit to test_dataset.py
patrick-naylor Oct 21, 2022
d24ba77
Apply suggestions from code review
Illviljan Oct 22, 2022
31876a5
Merge and rename reductions and cumulatives to aggregations
Illviljan Oct 22, 2022
d0ff135
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 22, 2022
4baa9f7
minor tweaks
Illviljan Oct 22, 2022
9b71aad
Merge branch 'cumulative_examples' of https://github.com/patrick-nayl…
Illviljan Oct 22, 2022
acb0637
Update computation.py
Illviljan Oct 22, 2022
47b80f3
Update generate_aggregations.py
Illviljan Oct 22, 2022
4994a07
Update .pre-commit-config.yaml
Illviljan Oct 22, 2022
29dd1eb
fix mypy
Illviljan Oct 22, 2022
b12dd5a
use _group_dim in resample?
Illviljan Oct 22, 2022
b4ce34d
Update resample.py
Illviljan Oct 22, 2022
a2d9e93
Manually fix docstring
Illviljan Oct 22, 2022
061ceaf
Merge branch 'main' into pr/7152
Illviljan Oct 23, 2022
2134bcf
Apply suggestions from code review
Illviljan Oct 24, 2022
f6e18cc
Use TEMPLATE_SEE_ALSO
Illviljan Oct 24, 2022
23df2eb
Merge branch 'cumulative_examples' of https://github.com/patrick-nayl…
Illviljan Oct 24, 2022
ec5062a
use default example
Illviljan Oct 24, 2022
1a7ccf4
add resample test
Illviljan Oct 24, 2022
66e7390
remove cumulative function in ops
Illviljan Oct 24, 2022
7bc95ec
Revert "remove cumulative function in ops"
Illviljan Oct 24, 2022
21e040c
Add numeric_only=True
Illviljan Oct 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated cumulatives to fix mypy issues
patrick-naylor committed Oct 10, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit ae513d9805b8aeb507a2d1ea4f1ab7f1e6456a3b
40 changes: 26 additions & 14 deletions xarray/core/_cumulatives.py
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ def cumsum(
If True, the attributes (`attrs`) will be copied from the original
object to the new one. If False (default), the new object will be
returned without attributes.
**kwargs : dict
**kwargs : Any
Additional keyword arguments passed on to `cumsum`.

Returns
@@ -122,8 +122,10 @@ def cumsum(

"""
return self.reduce(
duck_array_ops.cumsum,
func=duck_array_ops.cumsum,
dim=dim,
axis=axis,
skipna=skipna,
keep_attrs=keep_attrs,
**kwargs,
)
@@ -155,7 +157,7 @@ def cumprod(
If True, the attributes (`attrs`) will be copied from the original
object to the new one. If False (default), the new object will be
returned without attributes.
**kwargs : dict
**kwargs : Any
Additional keyword arguments passed on to `cumprod`.

Returns
@@ -215,8 +217,10 @@ def cumprod(

"""
return self.reduce(
duck_array_ops.cumprod,
func=duck_array_ops.cumprod,
dim=dim,
axis=axis,
skipna=skipna,
keep_attrs=keep_attrs,
**kwargs,
)
@@ -264,7 +268,7 @@ def cumsum(
If True, the attributes (`attrs`) will be copied from the original
object to the new one. If False (default), the new object will be
returned without attributes.
**kwargs : dict
**kwargs : Any
Additional keyword arguments passed on to `cumsum`.

Returns
@@ -333,8 +337,10 @@ def cumsum(

"""
return self.reduce(
duck_array_ops.cumsum,
func=duck_array_ops.cumsum,
dim=dim,
axis=axis,
skipna=skipna,
keep_attrs=keep_attrs,
**kwargs,
)
@@ -366,7 +372,7 @@ def cumprod(
If True, the attributes (`attrs`) will be copied from the original
object to the new one. If False (default), the new object will be
returned without attributes.
**kwargs : dict
**kwargs : Any
Additional keyword arguments passed on to `cumprod`.

Returns
@@ -435,8 +441,10 @@ def cumprod(

"""
return self.reduce(
duck_array_ops.cumprod,
func=duck_array_ops.cumprod,
dim=dim,
axis=axis,
skipna=skipna,
keep_attrs=keep_attrs,
**kwargs,
)
@@ -451,6 +459,7 @@ def reduce(
dim: Dims | ellipsis = None,
*,
axis: int | Sequence[int] | None = None,
skipna: bool | None = None,
keep_attrs: bool | None = None,
keepdims: bool = False,
**kwargs: Any,
@@ -491,7 +500,7 @@ def cumsum(
If True, the attributes (`attrs`) will be copied from the original
object to the new one. If False (default), the new object will be
returned without attributes.
**kwargs : dict
**kwargs : Any
Additional keyword arguments passed on to `cumsum`.

Returns
@@ -564,9 +573,10 @@ def cumsum(
)
else:
return self.reduce(
duck_array_ops.cumsum,
func=duck_array_ops.cumsum,
dim=dim,
keep_attrs=keep_attrs,
axis=axis,
skipna=skipna,
**kwargs,
)

@@ -580,6 +590,7 @@ def reduce(
dim: Dims | ellipsis = None,
*,
axis: int | Sequence[int] | None = None,
skipna: bool | None = None,
keep_attrs: bool | None = None,
keepdims: bool = False,
**kwargs: Any,
@@ -620,7 +631,7 @@ def cumsum(
If True, the attributes (`attrs`) will be copied from the original
object to the new one. If False (default), the new object will be
returned without attributes.
**kwargs : dict
**kwargs : Any
Additional keyword arguments passed on to `cumsum`.

Returns
@@ -698,8 +709,9 @@ def cumsum(
)
else:
return self.reduce(
duck_array_ops.cumsum,
func=duck_array_ops.cumsum,
dim=dim,
keep_attrs=keep_attrs,
axis=axis,
skipna=skipna,
**kwargs,
)
25 changes: 12 additions & 13 deletions xarray/util/generate_cumulatives.py
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Any, Callable, Sequence, Dict
from typing import TYPE_CHECKING, Any, Callable, Sequence

from . import duck_array_ops
from .options import OPTIONS
@@ -67,6 +67,7 @@ def reduce(
dim: Dims | ellipsis = None,
*,
axis: int | Sequence[int] | None = None,
skipna: bool | None = None,
keep_attrs: bool | None = None,
keepdims: bool = False,
**kwargs: Any,
@@ -134,7 +135,7 @@ def {method}(
object to the new one. If False (default), the new object will be
returned without attributes."""

_KWARGS_DOCSTRING = """**kwargs : dict
_KWARGS_DOCSTRING = """**kwargs : Any
Additional keyword arguments passed on to `{method}`."""


@@ -179,10 +180,8 @@ def generate_methods(self):

def generate_method(self, method):
template_kwargs = dict(obj=self.datastructure.name, method=method.name)
extra_kwargs = ""
yield self._template_signature.format(
**template_kwargs,
extra_kwargs=extra_kwargs,
cls=self.cls,
)

@@ -243,34 +242,34 @@ class GroupByReductionGenerator(ReductionGenerator):

def generate_code(self, method):

extra_kwargs = ""

return f"""\
if flox and OPTIONS["use_flox"] and contains_only_dask_or_numpy(self._obj):
return self._flox_reduce(
func="{method.name}",
dim=dim,{extra_kwargs}
dim=dim,
# fill_value=fill_value,
keep_attrs=keep_attrs,
**kwargs,
)
else:
return self.reduce(
duck_array_ops.{method.array_method},
dim=dim,{extra_kwargs}
keep_attrs=keep_attrs,
func=duck_array_ops.{method.array_method},
dim=dim,
axis=axis,
skipna=skipna,
**kwargs,
)"""


class GenericReductionGenerator(ReductionGenerator):
def generate_code(self, method):

extra_kwargs = ""
return f"""\
return self.reduce(
duck_array_ops.{method.array_method},
dim=dim,{extra_kwargs}
func=duck_array_ops.{method.array_method},
dim=dim,
axis=axis,
skipna=skipna,
keep_attrs=keep_attrs,
**kwargs,
)"""