Skip to content
This repository was archived by the owner on Oct 24, 2024. It is now read-only.
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
3 changes: 2 additions & 1 deletion datatree/datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,8 @@ def _copy_subtree(
"""Copy entire subtree"""
new_tree = self._copy_node(deep=deep)
for node in self.descendants:
new_tree[node.path] = node._copy_node(deep=deep)
path = node.relative_to(self)
new_tree[path] = node._copy_node(deep=deep)
return new_tree

def _copy_node(
Expand Down
8 changes: 8 additions & 0 deletions datatree/tests/test_datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ def test_copy(self, create_test_datatree):
assert "foo" not in node.attrs
assert node.attrs["Test"] is copied_node.attrs["Test"]

def test_copy_subtree(self):
dt = DataTree.from_dict({"/level1/level2/level3": xr.Dataset()})

actual = dt["/level1/level2"].copy()
expected = DataTree.from_dict({"/level3": xr.Dataset()}, name="level2")

dtt.assert_identical(actual, expected)

def test_deepcopy(self, create_test_datatree):
dt = create_test_datatree()

Expand Down
2 changes: 2 additions & 0 deletions docs/source/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Bug fixes

- Allow for altering of given dataset inside function called by :py:func:`map_over_subtree` (:issue:`188`, :pull:`194`).
By `Tom Nicholas <https://github.com/TomNicholas>`_.
- copy subtrees without creating ancestor nodes (:pull:`201`)
By `Justus Magin <https://github.com/keewis>`_.

Documentation
~~~~~~~~~~~~~
Expand Down