From f62f16de223840758e7a99bc407ba50feb0b411e Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Wed, 13 Feb 2019 14:51:59 +0000 Subject: [PATCH 1/4] New test for reduce func which takes no axes --- xarray/tests/test_dataset.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index e4ffdad4260..c26968b1db0 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -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): From ea646a410f827f547ca8c9d4dac55706a0631228 Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Wed, 13 Feb 2019 14:53:08 +0000 Subject: [PATCH 2/4] Fixed axis logic --- xarray/core/variable.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 85eab294619..a638dce13d3 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -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) if getattr(data, 'shape', ()) == self.shape: dims = self.dims From d0bd7b0fd09cf1894ad02f3bcc2ab3596211091f Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Wed, 13 Feb 2019 15:18:43 +0000 Subject: [PATCH 3/4] Recorded fix in what's new --- doc/whats-new.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 1ccddfb2cd6..a5a32461a57 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -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 `_. +- Fixed error when trying to reduce a DataArray using a function which does not + require an axis argument. (:issue:`2768`) + By `Tom Nicholas `_. + + .. _whats-new.0.11.3: v0.11.3 (26 January 2019) From d8fcb0a746e634f1534deec3003e8d325af35268 Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Fri, 15 Feb 2019 22:29:37 +0000 Subject: [PATCH 4/4] Added intermediate variable --- xarray/core/variable.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index a638dce13d3..b675317d83d 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -1361,11 +1361,11 @@ def reduce(self, func, dim=None, axis=None, if dim is not None: axis = self.get_axis_num(dim) + input_data = self.data if allow_lazy else self.values if axis is not None: - data = func(self.data if allow_lazy else self.values, - axis=axis, **kwargs) + data = func(input_data, axis=axis, **kwargs) else: - data = func(self.data if allow_lazy else self.values, **kwargs) + data = func(input_data, **kwargs) if getattr(data, 'shape', ()) == self.shape: dims = self.dims