Skip to content
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

use keep_attrs in binary operations II #2590

Merged
merged 8 commits into from
Dec 12, 2018
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
5 changes: 4 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Enhancements
:py:class:`pandas.DatetimeIndex`), which avoids unnecessary copies.
By `Stephan Hoyer <https://github.com/shoyer>`_


Bug fixes
~~~~~~~~~

Expand Down Expand Up @@ -150,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 <https://github.com/jhamman>`_.

- ``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 <https://github.com/MBlaschek>`_

Bug fixes
~~~~~~~~~

Expand Down
4 changes: 3 additions & 1 deletion xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions xarray/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
assert_allclose, assert_array_equal, assert_equal, assert_identical,
raises_regex, requires_dask, source_ndarray)

from xarray import set_options


class VariableSubclassobjects(object):
def test_properties(self):
Expand Down Expand Up @@ -1545,6 +1547,18 @@ def test_reduce_keep_attrs(self):
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 d.attrs == OrderedDict()
# Test kept attrs
with set_options(keep_attrs=True):
d = a - b
assert d.attrs == _attrs

def test_count(self):
expected = Variable([], 3)
actual = Variable(['x'], [1, 2, 3, np.nan]).count()
Expand Down