Skip to content

Bugfix/reduce no axis #2769

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

Merged
merged 5 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ Bug fixes
- Masking data arrays with :py:meth:`xarray.DataArray.where` now returns an
array with the name of the original masked array (:issue:`2748` and :issue:`2457`).
By `Yohai Bar-Sinai <https://github.com/yohai>`_.
- Fixed error when trying to reduce a DataArray using a function which does not
require an axis argument. (:issue:`2768`)
By `Tom Nicholas <http://github.com/TomNicholas>`_.

- Per `CF conventions
<http://cfconventions.org/cf-conventions/cf-conventions.html#calendar>`_,
specifying ``'standard'`` as the calendar type in
Expand Down
7 changes: 5 additions & 2 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1361,8 +1361,11 @@ def reduce(self, func, dim=None, axis=None,

if dim is not None:
axis = self.get_axis_num(dim)
data = func(self.data if allow_lazy else self.values,
axis=axis, **kwargs)
input_data = self.data if allow_lazy else self.values
if axis is not None:
data = func(input_data, axis=axis, **kwargs)
else:
data = func(input_data, **kwargs)

if getattr(data, 'shape', ()) == self.shape:
dims = self.dims
Expand Down
21 changes: 19 additions & 2 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3633,11 +3633,28 @@ def mean_only_one_axis(x, axis):
actual = ds.reduce(mean_only_one_axis, 'y')
assert_identical(expected, actual)

with raises_regex(TypeError, 'non-integer axis'):
with raises_regex(TypeError, "missing 1 required positional argument: "
"'axis'"):
ds.reduce(mean_only_one_axis)

with raises_regex(TypeError, 'non-integer axis'):
ds.reduce(mean_only_one_axis, ['x', 'y'])
ds.reduce(mean_only_one_axis, axis=['x', 'y'])

def test_reduce_no_axis(self):

def total_sum(x):
return np.sum(x.flatten())

ds = Dataset({'a': (['x', 'y'], [[0, 1, 2, 3, 4]])})
expected = Dataset({'a': ((), 10)})
actual = ds.reduce(total_sum)
assert_identical(expected, actual)

with raises_regex(TypeError, "unexpected keyword argument 'axis'"):
ds.reduce(total_sum, axis=0)

with raises_regex(TypeError, "unexpected keyword argument 'axis'"):
ds.reduce(total_sum, dim='x')

def test_quantile(self):

Expand Down