Skip to content

Commit 5ea724b

Browse files
committed
Explore moving functions from reduce to apply_ufunc
1 parent fb9c9b3 commit 5ea724b

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

xarray/core/rolling_exp.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import numpy as np
77

8+
from xarray.core.computation import apply_ufunc
89
from xarray.core.options import _get_keep_attrs
910
from xarray.core.pdcompat import count_not_none
1011
from xarray.core.pycompat import is_duck_dask_array
@@ -128,9 +129,18 @@ def mean(self, keep_attrs: bool | None = None) -> T_DataWithCoords:
128129
if keep_attrs is None:
129130
keep_attrs = _get_keep_attrs(default=True)
130131

131-
return self.obj.reduce(
132-
move_exp_nanmean, dim=self.dim, alpha=self.alpha, keep_attrs=keep_attrs
133-
)
132+
dim_order = self.obj.dims
133+
134+
return apply_ufunc(
135+
move_exp_nanmean,
136+
self.obj,
137+
input_core_dims=[[self.dim]],
138+
kwargs=dict(alpha=self.alpha, axis=-1),
139+
output_core_dims=[[self.dim]],
140+
exclude_dims={self.dim},
141+
keep_attrs=keep_attrs,
142+
on_missing_core_dim="copy",
143+
).transpose(*dim_order)
134144

135145
def sum(self, keep_attrs: bool | None = None) -> T_DataWithCoords:
136146
"""
@@ -155,6 +165,15 @@ def sum(self, keep_attrs: bool | None = None) -> T_DataWithCoords:
155165
if keep_attrs is None:
156166
keep_attrs = _get_keep_attrs(default=True)
157167

158-
return self.obj.reduce(
159-
move_exp_nansum, dim=self.dim, alpha=self.alpha, keep_attrs=keep_attrs
160-
)
168+
dim_order = self.obj.dims
169+
170+
return apply_ufunc(
171+
move_exp_nansum,
172+
self.obj,
173+
input_core_dims=[[self.dim]],
174+
kwargs=dict(alpha=self.alpha, axis=-1),
175+
output_core_dims=[[self.dim]],
176+
exclude_dims={self.dim},
177+
keep_attrs=keep_attrs,
178+
on_missing_core_dim="copy",
179+
).transpose(*dim_order)

0 commit comments

Comments
 (0)