Skip to content

Commit 9396605

Browse files
gladclefBenjamin Beandcherianmax-sixty
authored
Make a copy of the attrs in the merged object (#4629)
* fixes bug #4627 to make a copy of the attrs in the merged object * Better formatting in doc/whats-new.rst Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com> Co-authored-by: Benjamin Bean <bbean@nrao.edu> Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com> Co-authored-by: Maximilian Roos <m@maxroos.com>
1 parent a41edc7 commit 9396605

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

doc/whats-new.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ New Features
3131
Bug fixes
3232
~~~~~~~~~
3333

34+
- :py:func:`merge` with ``combine_attrs='override'`` makes a copy of the attrs (:issue:`4627`).
3435

3536
Documentation
3637
~~~~~~~~~~~~~

xarray/core/merge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ def merge_attrs(variable_attrs, combine_attrs):
501501
if combine_attrs == "drop":
502502
return {}
503503
elif combine_attrs == "override":
504-
return variable_attrs[0]
504+
return dict(variable_attrs[0])
505505
elif combine_attrs == "no_conflicts":
506506
result = dict(variable_attrs[0])
507507
for attrs in variable_attrs[1:]:

xarray/tests/test_merge.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ def test_merge_arrays_attrs(
109109
expected.attrs = expected_attrs
110110
assert actual.identical(expected)
111111

112+
def test_merge_attrs_override_copy(self):
113+
ds1 = xr.Dataset(attrs={"x": 0})
114+
ds2 = xr.Dataset(attrs={"x": 1})
115+
ds3 = xr.merge([ds1, ds2], combine_attrs="override")
116+
ds3.attrs["x"] = 2
117+
assert ds1.x == 0
118+
112119
def test_merge_dicts_simple(self):
113120
actual = xr.merge([{"foo": 0}, {"bar": "one"}, {"baz": 3.5}])
114121
expected = xr.Dataset({"foo": 0, "bar": "one", "baz": 3.5})

0 commit comments

Comments
 (0)