Skip to content

Commit 97d7e76

Browse files
arongergelykeewisdcherian
authored
Follow keep_attrs in Dataset binary ops (#7391)
* honor keep_attrs option durig Dataset binary ops * add test for Dataset binary ops keep_attrs * fix mypy assignment error * Apply suggestions from code review accept non-boolean keep_attrs arguments Co-authored-by: Justus Magin <keewis@users.noreply.github.com> * test all cases for binary_ops keep_attrs * fix import * add keep_attrs bugfix to whats-new.rst * accept only boolean keep_attrs in binary_ops * Update whats-new.rst Co-authored-by: Justus Magin <keewis@users.noreply.github.com> Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
1 parent ccbdd0d commit 97d7e76

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Bug fixes
5353
- :py:func:`xarray.concat` can now concatenate variables present in some datasets but
5454
not others (:issue:`508`, :pull:`7400`).
5555
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_ and `Scott Chamberlin <https://github.com/scottcha>`_.
56+
- Handle ``keep_attrs`` option in binary operators of :py:meth:`Dataset` (:issue:`7390`, :pull:`7391`).
57+
By `Aron Gergely <https://github.com/arongergely>`_.
5658

5759
Documentation
5860
~~~~~~~~~~~~~

xarray/core/dataset.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6585,6 +6585,9 @@ def _binary_op(self, other, f, reflexive=False, join=None) -> Dataset:
65856585
self, other = align(self, other, join=align_type, copy=False) # type: ignore[assignment]
65866586
g = f if not reflexive else lambda x, y: f(y, x)
65876587
ds = self._calculate_binary_op(g, other, join=align_type)
6588+
keep_attrs = _get_keep_attrs(default=False)
6589+
if keep_attrs:
6590+
ds.attrs = self.attrs
65886591
return ds
65896592

65906593
def _inplace_binary_op(self: T_Dataset, other, f) -> T_Dataset:

xarray/tests/test_dataset.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5850,6 +5850,21 @@ def test_binary_op_join_setting(self) -> None:
58505850
actual = ds1 + ds2
58515851
assert_equal(actual, expected)
58525852

5853+
@pytest.mark.parametrize(
5854+
["keep_attrs", "expected"],
5855+
(
5856+
pytest.param(False, {}, id="False"),
5857+
pytest.param(True, {"foo": "a", "bar": "b"}, id="True"),
5858+
),
5859+
)
5860+
def test_binary_ops_keep_attrs(self, keep_attrs, expected) -> None:
5861+
ds1 = xr.Dataset({"a": 1}, attrs={"foo": "a", "bar": "b"})
5862+
ds2 = xr.Dataset({"a": 1}, attrs={"foo": "a", "baz": "c"})
5863+
with xr.set_options(keep_attrs=keep_attrs):
5864+
ds_result = ds1 + ds2
5865+
5866+
assert ds_result.attrs == expected
5867+
58535868
def test_full_like(self) -> None:
58545869
# For more thorough tests, see test_variable.py
58555870
# Note: testing data_vars with mismatched dtypes

0 commit comments

Comments
 (0)