From 46b97108e67c1bcd790cf719e4074de585ccdc34 Mon Sep 17 00:00:00 2001 From: MBlaschek Date: Mon, 3 Dec 2018 14:58:33 +0100 Subject: [PATCH 1/7] Add keep_attrs to binary_op --- xarray/core/variable.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 0bff06e7546..7a921805258 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -1658,11 +1658,13 @@ def func(self, other): if isinstance(other, (xr.DataArray, xr.Dataset)): return NotImplemented self_data, other_data, dims = _broadcast_compat_data(self, other) + keep_attrs = _get_keep_attrs(default=False) + attrs = self._attrs if keep_attrs else None with np.errstate(all='ignore'): new_data = (f(self_data, other_data) if not reflexive else f(other_data, self_data)) - result = Variable(dims, new_data) + result = Variable(dims, new_data, attrs=attrs) return result return func From 56aeaaab2bf84d99db58aa2bc72e9fac0af81974 Mon Sep 17 00:00:00 2001 From: MBlaschek Date: Wed, 5 Dec 2018 10:43:28 +0100 Subject: [PATCH 2/7] added test for binary_ops keep_attrs=True --- xarray/tests/test_variable.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 0bd440781ac..995a50d0d67 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -26,7 +26,7 @@ from . import ( assert_allclose, assert_array_equal, assert_equal, assert_identical, - raises_regex, requires_dask, source_ndarray) + raises_regex, requires_dask, source_ndarray, set_options) class VariableSubclassobjects(object): @@ -1544,7 +1544,24 @@ def test_reduce_keep_attrs(self): vm = v.mean(keep_attrs=True) assert len(vm.attrs) == len(_attrs) assert vm.attrs == _attrs + + def test_binary_ops_keep_attrs(self): + _attrs = {'units': 'test', 'long_name': 'testing'} + + a = Variable(['x', 'y'], np.random.randn(3,3), _attrs) + b = Variable(['x', 'y'], np.random.randn(3,3), _attrs) + # Test dropped attrs + d = a - b # just one operation + assert len(d.attrs) == 0 + assert d.attrs == OrderedDict() + + # Test kept attrs + with set_options(keep_attrs=True): + d = a - b + assert len(d.attrs) == len(_attrs) + assert d.attrs == _attrs + def test_count(self): expected = Variable([], 3) actual = Variable(['x'], [1, 2, 3, np.nan]).count() From 8d4dabe428c08569829a5b0fb1620ff908fe1405 Mon Sep 17 00:00:00 2001 From: MBlaschek Date: Fri, 7 Dec 2018 10:38:17 +0100 Subject: [PATCH 3/7] PEP8 issues + blank lines removed --- xarray/tests/test_variable.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 995a50d0d67..4b7f638f9e2 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -26,7 +26,9 @@ from . import ( assert_allclose, assert_array_equal, assert_equal, assert_identical, - raises_regex, requires_dask, source_ndarray, set_options) + raises_regex, requires_dask, source_ndarray) + +from xarray import set_options class VariableSubclassobjects(object): @@ -1547,21 +1549,18 @@ def test_reduce_keep_attrs(self): def test_binary_ops_keep_attrs(self): _attrs = {'units': 'test', 'long_name': 'testing'} - - a = Variable(['x', 'y'], np.random.randn(3,3), _attrs) - b = Variable(['x', 'y'], np.random.randn(3,3), _attrs) - + a = Variable(['x', 'y'], np.random.randn(3, 3), _attrs) + b = Variable(['x', 'y'], np.random.randn(3, 3), _attrs) # Test dropped attrs d = a - b # just one operation assert len(d.attrs) == 0 assert d.attrs == OrderedDict() - # Test kept attrs with set_options(keep_attrs=True): - d = a - b + d = a - b assert len(d.attrs) == len(_attrs) assert d.attrs == _attrs - + def test_count(self): expected = Variable([], 3) actual = Variable(['x'], [1, 2, 3, np.nan]).count() From 80cca11e67d14774a0193e3227a57a7b7d724de4 Mon Sep 17 00:00:00 2001 From: MBlaschek Date: Fri, 7 Dec 2018 10:40:04 +0100 Subject: [PATCH 4/7] whitespace removed --- xarray/tests/test_variable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 4b7f638f9e2..684fd6faa65 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -1546,7 +1546,7 @@ def test_reduce_keep_attrs(self): vm = v.mean(keep_attrs=True) assert len(vm.attrs) == len(_attrs) assert vm.attrs == _attrs - + def test_binary_ops_keep_attrs(self): _attrs = {'units': 'test', 'long_name': 'testing'} a = Variable(['x', 'y'], np.random.randn(3, 3), _attrs) From 109b19f5432faba44a82d3766764f61d623086be Mon Sep 17 00:00:00 2001 From: MBlaschek Date: Fri, 7 Dec 2018 10:56:08 +0100 Subject: [PATCH 5/7] keep_attrs in DataArray --- doc/whats-new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index b1d5b92da4d..2006c0552ba 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -39,6 +39,8 @@ Enhancements - :py:class:`CFTimeIndex` uses slicing for string indexing when possible (like :py:class:`pandas.DatetimeIndex`), which avoids unnecessary copies. By `Stephan Hoyer `_ +- ``DataArray`` can now use ``xr.set_option(keep_attrs=True)`` and retain attributes in binary operations, + such as (``+, -, * ,/``). Default behaviour is unchanged (*Attributes will be dismissed*). By `Michael Blaschek `_ Bug fixes ~~~~~~~~~ From 7ffdd986c866fd982aa3f1ed04137b7f3c4d7905 Mon Sep 17 00:00:00 2001 From: MBlaschek Date: Wed, 12 Dec 2018 11:51:31 +0100 Subject: [PATCH 6/7] enhancement --- doc/whats-new.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 2006c0552ba..05973bbd5f0 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -39,8 +39,7 @@ Enhancements - :py:class:`CFTimeIndex` uses slicing for string indexing when possible (like :py:class:`pandas.DatetimeIndex`), which avoids unnecessary copies. By `Stephan Hoyer `_ -- ``DataArray`` can now use ``xr.set_option(keep_attrs=True)`` and retain attributes in binary operations, - such as (``+, -, * ,/``). Default behaviour is unchanged (*Attributes will be dismissed*). By `Michael Blaschek `_ + Bug fixes ~~~~~~~~~ @@ -152,7 +151,9 @@ Enhancements to returning (and is now deprecated). This was changed in order to facilitate using tutorial datasets with dask. By `Joe Hamman `_. - +- ``DataArray`` can now use ``xr.set_option(keep_attrs=True)`` and retain attributes in binary operations, + such as (``+, -, * ,/``). Default behaviour is unchanged (*Attributes will be dismissed*). By `Michael Blaschek `_ + Bug fixes ~~~~~~~~~ From e805f1bcd2c63de7fa24625983f9cfe9e300535a Mon Sep 17 00:00:00 2001 From: MBlaschek Date: Wed, 12 Dec 2018 11:54:14 +0100 Subject: [PATCH 7/7] simpler testing --- xarray/tests/test_variable.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 684fd6faa65..84813f6c918 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -1553,12 +1553,10 @@ def test_binary_ops_keep_attrs(self): b = Variable(['x', 'y'], np.random.randn(3, 3), _attrs) # Test dropped attrs d = a - b # just one operation - assert len(d.attrs) == 0 assert d.attrs == OrderedDict() # Test kept attrs with set_options(keep_attrs=True): d = a - b - assert len(d.attrs) == len(_attrs) assert d.attrs == _attrs def test_count(self):