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

Deprecate allow_lazy #3435

Merged
merged 17 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
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
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ Bug fixes
By `Deepak Cherian <https://github.com/dcherian>`_.
- Sync with cftime by removing `dayofwk=-1` for cftime>=1.0.4.
By `Anderson Banihirwe <https://github.com/andersy005>`_.
- Rolling reduction operations now longer compute dask arrays by default. (:issue:3161).
dcherian marked this conversation as resolved.
Show resolved Hide resolved
In addition, the ``allow_lazy`` kwarg to ``reduce`` is deprecated.
By `Deepak Cherian <https://github.com/dcherian>`_.
- Fix :py:meth:`xarray.core.groupby.DataArrayGroupBy.reduce` and
:py:meth:`xarray.core.groupby.DatasetGroupBy.reduce` when reducing over multiple dimensions.
(:issue:`3402`). By `Deepak Cherian <https://github.com/dcherian/>`_
Expand Down
17 changes: 4 additions & 13 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ def _reduce_method(cls, func: Callable, include_skipna: bool, numeric_only: bool
if include_skipna:

def wrapped_func(self, dim=None, axis=None, skipna=None, **kwargs):
return self.reduce(
func, dim, axis, skipna=skipna, allow_lazy=True, **kwargs
)
return self.reduce(func, dim, axis, skipna=skipna, **kwargs)

else:

def wrapped_func(self, dim=None, axis=None, **kwargs): # type: ignore
return self.reduce(func, dim, axis, allow_lazy=True, **kwargs)
return self.reduce(func, dim, axis, **kwargs)

return wrapped_func

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

def wrapped_func(self, dim=None, skipna=None, **kwargs):
return self.reduce(
func,
dim,
skipna=skipna,
numeric_only=numeric_only,
allow_lazy=True,
**kwargs,
func, dim, skipna=skipna, numeric_only=numeric_only, **kwargs
)

else:

def wrapped_func(self, dim=None, **kwargs): # type: ignore
return self.reduce(
func, dim, numeric_only=numeric_only, allow_lazy=True, **kwargs
)
return self.reduce(func, dim, numeric_only=numeric_only, **kwargs)

return wrapped_func

Expand Down
2 changes: 1 addition & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4022,7 +4022,7 @@ def reduce(
keep_attrs: bool = None,
keepdims: bool = False,
numeric_only: bool = False,
allow_lazy: bool = False,
allow_lazy: bool = None,
**kwargs: Any,
) -> "Dataset":
"""Reduce this dataset by applying `func` along some dimension(s).
Expand Down
4 changes: 1 addition & 3 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,7 @@ def _first_or_last(self, op, skipna, keep_attrs):
return self._obj
if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=True)
return self.reduce(
op, self._group_dim, skipna=skipna, keep_attrs=keep_attrs, allow_lazy=True
)
return self.reduce(op, self._group_dim, skipna=skipna, keep_attrs=keep_attrs)

def first(self, skipna=None, keep_attrs=None):
"""Return the first element of each group along the group dimension
Expand Down
13 changes: 12 additions & 1 deletion xarray/core/variable.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import functools
import itertools
import warnings
from collections import defaultdict
from datetime import timedelta
from distutils.version import LooseVersion
Expand Down Expand Up @@ -1425,7 +1426,7 @@ def reduce(
axis=None,
keep_attrs=None,
keepdims=False,
allow_lazy=False,
allow_lazy=None,
**kwargs,
):
"""Reduce this array by applying `func` along some dimension(s).
Expand Down Expand Up @@ -1466,7 +1467,17 @@ def reduce(

if dim is not None:
axis = self.get_axis_num(dim)

if allow_lazy is not None:
warnings.warn(
"allow_lazy will be deprecated in version 0.16.0. It is now True by default.",
dcherian marked this conversation as resolved.
Show resolved Hide resolved
DeprecationWarning,
)
else:
allow_lazy = True

input_data = self.data if allow_lazy else self.values

if axis is not None:
data = func(input_data, axis=axis, **kwargs)
else:
Expand Down
17 changes: 15 additions & 2 deletions xarray/tests/test_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ def test_reduce(self):
self.assertLazyAndAllClose((u < 1).all("x"), (v < 1).all("x"))
with raises_regex(NotImplementedError, "dask"):
v.median()
with raise_if_dask_computes():
v.reduce(np.mean)

def test_missing_values(self):
values = np.array([0, 1, np.nan, 3])
Expand Down Expand Up @@ -488,7 +490,17 @@ def test_groupby(self):
v = self.lazy_array

expected = u.groupby("x").mean(...)
actual = v.groupby("x").mean(...)
with raise_if_dask_computes():
actual = v.groupby("x").mean(...)
self.assertLazyAndAllClose(expected, actual)

def test_rolling(self):
u = self.eager_array
v = self.lazy_array

expected = u.rolling(x=2).mean()
with raise_if_dask_computes():
actual = v.rolling(x=2).mean()
self.assertLazyAndAllClose(expected, actual)

def test_groupby_first(self):
Expand All @@ -500,7 +512,8 @@ def test_groupby_first(self):
with raises_regex(NotImplementedError, "dask"):
v.groupby("ab").first()
expected = u.groupby("ab").first()
actual = v.groupby("ab").first(skipna=False)
with raise_if_dask_computes():
actual = v.groupby("ab").first(skipna=False)
self.assertLazyAndAllClose(expected, actual)

def test_reindex(self):
Expand Down
4 changes: 4 additions & 0 deletions xarray/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,10 @@ def test_reduce(self):

with raises_regex(ValueError, "cannot supply both"):
v.mean(dim="x", axis=0)
with pytest.warns(DeprecationWarning, match="allow_lazy will be deprecated"):
v.mean("x", allow_lazy=True)
with pytest.warns(DeprecationWarning, match="allow_lazy will be deprecated"):
v.mean("x", allow_lazy=False)

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