Skip to content

Commit 6c56b12

Browse files
committed
fix by shallow copying
1 parent 4b7e367 commit 6c56b12

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

xarray/core/datatree.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,10 @@ def __init__(
463463

464464
super().__init__(name=name)
465465
self._set_node_data(_coerce_to_dataset(data))
466-
self.parent = parent
467-
self.children = children
466+
467+
# shallow copy to avoid modifying arguments in-place (see GH issue #9196)
468+
self.parent = parent.copy() if parent is not None else None
469+
self.children = {name: child.copy() for name, child in children.items()}
468470

469471
def _set_node_data(self, ds: Dataset):
470472
data_vars, coord_vars = _collect_data_and_coord_variables(ds)
@@ -770,7 +772,7 @@ def _replace_node(
770772
if data is not _default:
771773
self._set_node_data(ds)
772774

773-
self._children = children
775+
self.children = children
774776

775777
def copy(
776778
self: DataTree,

0 commit comments

Comments
 (0)