Skip to content

Commit 94525bb

Browse files
authored
Deprecate allow_lazy (#3435)
* Deprecate allow_lazy * add whats-new * test that reductions are lazy * minor whats-new fix. * fix merge wahts=new * fix bad merge. * remove tests that only work with nep-18 * Update doc/whats-new.rst Co-Authored-By: Mathias Hauser <mathause@users.noreply.github.com> * Update xarray/core/variable.py Co-Authored-By: Mathias Hauser <mathause@users.noreply.github.com> * fix whats-new * Fix test that assumed NEP-18 * fix tests.
1 parent e70138b commit 94525bb

File tree

7 files changed

+41
-20
lines changed

7 files changed

+41
-20
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ Bug fixes
8888
By `Deepak Cherian <https://github.com/dcherian>`_.
8989
- Sync with cftime by removing `dayofwk=-1` for cftime>=1.0.4.
9090
By `Anderson Banihirwe <https://github.com/andersy005>`_.
91+
- Rolling reduction operations no longer compute dask arrays by default. (:issue:`3161`).
92+
In addition, the ``allow_lazy`` kwarg to ``reduce`` is deprecated.
93+
By `Deepak Cherian <https://github.com/dcherian>`_.
9194
- Fix :py:meth:`xarray.core.groupby.DataArrayGroupBy.reduce` and
9295
:py:meth:`xarray.core.groupby.DatasetGroupBy.reduce` when reducing over multiple dimensions.
9396
(:issue:`3402`). By `Deepak Cherian <https://github.com/dcherian/>`_

xarray/core/common.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@ def _reduce_method(cls, func: Callable, include_skipna: bool, numeric_only: bool
4343
if include_skipna:
4444

4545
def wrapped_func(self, dim=None, axis=None, skipna=None, **kwargs):
46-
return self.reduce(
47-
func, dim, axis, skipna=skipna, allow_lazy=True, **kwargs
48-
)
46+
return self.reduce(func, dim, axis, skipna=skipna, **kwargs)
4947

5048
else:
5149

5250
def wrapped_func(self, dim=None, axis=None, **kwargs): # type: ignore
53-
return self.reduce(func, dim, axis, allow_lazy=True, **kwargs)
51+
return self.reduce(func, dim, axis, **kwargs)
5452

5553
return wrapped_func
5654

@@ -83,20 +81,13 @@ def _reduce_method(cls, func: Callable, include_skipna: bool, numeric_only: bool
8381

8482
def wrapped_func(self, dim=None, skipna=None, **kwargs):
8583
return self.reduce(
86-
func,
87-
dim,
88-
skipna=skipna,
89-
numeric_only=numeric_only,
90-
allow_lazy=True,
91-
**kwargs,
84+
func, dim, skipna=skipna, numeric_only=numeric_only, **kwargs
9285
)
9386

9487
else:
9588

9689
def wrapped_func(self, dim=None, **kwargs): # type: ignore
97-
return self.reduce(
98-
func, dim, numeric_only=numeric_only, allow_lazy=True, **kwargs
99-
)
90+
return self.reduce(func, dim, numeric_only=numeric_only, **kwargs)
10091

10192
return wrapped_func
10293

xarray/core/dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4031,7 +4031,7 @@ def reduce(
40314031
keep_attrs: bool = None,
40324032
keepdims: bool = False,
40334033
numeric_only: bool = False,
4034-
allow_lazy: bool = False,
4034+
allow_lazy: bool = None,
40354035
**kwargs: Any,
40364036
) -> "Dataset":
40374037
"""Reduce this dataset by applying `func` along some dimension(s).

xarray/core/groupby.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,7 @@ def _first_or_last(self, op, skipna, keep_attrs):
585585
return self._obj
586586
if keep_attrs is None:
587587
keep_attrs = _get_keep_attrs(default=True)
588-
return self.reduce(
589-
op, self._group_dim, skipna=skipna, keep_attrs=keep_attrs, allow_lazy=True
590-
)
588+
return self.reduce(op, self._group_dim, skipna=skipna, keep_attrs=keep_attrs)
591589

592590
def first(self, skipna=None, keep_attrs=None):
593591
"""Return the first element of each group along the group dimension

xarray/core/variable.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
import itertools
3+
import warnings
34
from collections import defaultdict
45
from datetime import timedelta
56
from distutils.version import LooseVersion
@@ -1427,7 +1428,7 @@ def reduce(
14271428
axis=None,
14281429
keep_attrs=None,
14291430
keepdims=False,
1430-
allow_lazy=False,
1431+
allow_lazy=None,
14311432
**kwargs,
14321433
):
14331434
"""Reduce this array by applying `func` along some dimension(s).
@@ -1468,7 +1469,17 @@ def reduce(
14681469

14691470
if dim is not None:
14701471
axis = self.get_axis_num(dim)
1472+
1473+
if allow_lazy is not None:
1474+
warnings.warn(
1475+
"allow_lazy is deprecated and will be removed in version 0.16.0. It is now True by default.",
1476+
DeprecationWarning,
1477+
)
1478+
else:
1479+
allow_lazy = True
1480+
14711481
input_data = self.data if allow_lazy else self.values
1482+
14721483
if axis is not None:
14731484
data = func(input_data, axis=axis, **kwargs)
14741485
else:

xarray/tests/test_dask.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import xarray as xr
1313
import xarray.ufuncs as xu
1414
from xarray import DataArray, Dataset, Variable
15+
from xarray.core import duck_array_ops
1516
from xarray.testing import assert_chunks_equal
1617
from xarray.tests import mock
1718

@@ -217,6 +218,8 @@ def test_reduce(self):
217218
self.assertLazyAndAllClose((u < 1).all("x"), (v < 1).all("x"))
218219
with raises_regex(NotImplementedError, "dask"):
219220
v.median()
221+
with raise_if_dask_computes():
222+
v.reduce(duck_array_ops.mean)
220223

221224
def test_missing_values(self):
222225
values = np.array([0, 1, np.nan, 3])
@@ -488,7 +491,17 @@ def test_groupby(self):
488491
v = self.lazy_array
489492

490493
expected = u.groupby("x").mean(...)
491-
actual = v.groupby("x").mean(...)
494+
with raise_if_dask_computes():
495+
actual = v.groupby("x").mean(...)
496+
self.assertLazyAndAllClose(expected, actual)
497+
498+
def test_rolling(self):
499+
u = self.eager_array
500+
v = self.lazy_array
501+
502+
expected = u.rolling(x=2).mean()
503+
with raise_if_dask_computes():
504+
actual = v.rolling(x=2).mean()
492505
self.assertLazyAndAllClose(expected, actual)
493506

494507
def test_groupby_first(self):
@@ -500,7 +513,8 @@ def test_groupby_first(self):
500513
with raises_regex(NotImplementedError, "dask"):
501514
v.groupby("ab").first()
502515
expected = u.groupby("ab").first()
503-
actual = v.groupby("ab").first(skipna=False)
516+
with raise_if_dask_computes():
517+
actual = v.groupby("ab").first(skipna=False)
504518
self.assertLazyAndAllClose(expected, actual)
505519

506520
def test_reindex(self):

xarray/tests/test_variable.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,10 @@ def test_reduce(self):
14771477

14781478
with raises_regex(ValueError, "cannot supply both"):
14791479
v.mean(dim="x", axis=0)
1480+
with pytest.warns(DeprecationWarning, match="allow_lazy is deprecated"):
1481+
v.mean(dim="x", allow_lazy=True)
1482+
with pytest.warns(DeprecationWarning, match="allow_lazy is deprecated"):
1483+
v.mean(dim="x", allow_lazy=False)
14801484

14811485
def test_quantile(self):
14821486
v = Variable(["x", "y"], self.d)

0 commit comments

Comments
 (0)