Skip to content

Commit 82789bc

Browse files
MBlaschekshoyer
authored andcommitted
use keep_attrs in binary operations II (#2590)
* Add keep_attrs to binary_op * added test for binary_ops keep_attrs=True * PEP8 issues + blank lines removed * whitespace removed * keep_attrs in DataArray * enhancement * simpler testing
1 parent 23483ad commit 82789bc

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

doc/whats-new.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Enhancements
4545
"dayofyear" and "dayofweek" accessors (:issue:`2597`). By `Spencer Clark
4646
<https://github.com/spencerkclark>`_.
4747

48+
4849
Bug fixes
4950
~~~~~~~~~
5051

@@ -158,7 +159,9 @@ Enhancements
158159
to returning (and is now deprecated). This was changed in order to facilitate
159160
using tutorial datasets with dask.
160161
By `Joe Hamman <https://github.com/jhamman>`_.
161-
162+
- ``DataArray`` can now use ``xr.set_option(keep_attrs=True)`` and retain attributes in binary operations,
163+
such as (``+, -, * ,/``). Default behaviour is unchanged (*Attributes will be dismissed*). By `Michael Blaschek <https://github.com/MBlaschek>`_
164+
162165
Bug fixes
163166
~~~~~~~~~
164167

xarray/core/variable.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1658,11 +1658,13 @@ def func(self, other):
16581658
if isinstance(other, (xr.DataArray, xr.Dataset)):
16591659
return NotImplemented
16601660
self_data, other_data, dims = _broadcast_compat_data(self, other)
1661+
keep_attrs = _get_keep_attrs(default=False)
1662+
attrs = self._attrs if keep_attrs else None
16611663
with np.errstate(all='ignore'):
16621664
new_data = (f(self_data, other_data)
16631665
if not reflexive
16641666
else f(other_data, self_data))
1665-
result = Variable(dims, new_data)
1667+
result = Variable(dims, new_data, attrs=attrs)
16661668
return result
16671669
return func
16681670

xarray/tests/test_variable.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
assert_allclose, assert_array_equal, assert_equal, assert_identical,
2929
raises_regex, requires_dask, source_ndarray)
3030

31+
from xarray import set_options
32+
3133

3234
class VariableSubclassobjects(object):
3335
def test_properties(self):
@@ -1545,6 +1547,18 @@ def test_reduce_keep_attrs(self):
15451547
assert len(vm.attrs) == len(_attrs)
15461548
assert vm.attrs == _attrs
15471549

1550+
def test_binary_ops_keep_attrs(self):
1551+
_attrs = {'units': 'test', 'long_name': 'testing'}
1552+
a = Variable(['x', 'y'], np.random.randn(3, 3), _attrs)
1553+
b = Variable(['x', 'y'], np.random.randn(3, 3), _attrs)
1554+
# Test dropped attrs
1555+
d = a - b # just one operation
1556+
assert d.attrs == OrderedDict()
1557+
# Test kept attrs
1558+
with set_options(keep_attrs=True):
1559+
d = a - b
1560+
assert d.attrs == _attrs
1561+
15481562
def test_count(self):
15491563
expected = Variable([], 3)
15501564
actual = Variable(['x'], [1, 2, 3, np.nan]).count()

0 commit comments

Comments
 (0)