Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ 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 <https://github.com/TomNicholas>`_.


.. _whats-new.0.11.3:

v0.11.3 (26 January 2019)
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)
if axis is not None:
data = func(self.data if allow_lazy else self.values,
axis=axis, **kwargs)
else:
data = func(self.data if allow_lazy else self.values, **kwargs)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This no longer passes axis=None into func, which I think is reasonable - it's generally the default. (but someone should confirm my thinking)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, though I would rewrite this to an intermediate variable for self.data if allow_lazy else self.values rather than writing it out twice

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, should be ready for merging now.


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