Skip to content
This repository was archived by the owner on Oct 24, 2024. It is now read-only.

copy subtrees without creating nodes for ancestors #201

Merged
merged 4 commits into from
Feb 10, 2023

Conversation

keewis
Copy link
Contributor

@keewis keewis commented Jan 18, 2023

Trying to copy subtrees leaves behind nodes for ancestors:

In [1]: import datatree
   ...: import xarray as xr
   ...: 
   ...: tree = datatree.DataTree.from_dict({"/level1/level2/level3": xr.Dataset()})
   ...: tree
Out[1]: 
DataTree('None', parent=None)
└── DataTree('level1')
    └── DataTree('level2')
        └── DataTree('level3')

In [2]: tree["/level1/level2"].copy()
Out[2]: 
DataTree('level2', parent=None)
└── DataTree('level1')
    └── DataTree('level2')
        └── DataTree('level3')

It seems this is caused by using absolute paths to copy over the descendants:

new_tree[node.path] = node._copy_node(deep=deep)

  • Tests added
  • Passes pre-commit run --all-files
  • Changes are summarized in docs/source/whats-new.rst

@TomNicholas
Copy link
Member

Thanks @keewis ! This was an oversight on my part - this change should have happened in #171

@keewis
Copy link
Contributor Author

keewis commented Feb 10, 2023

@TomNicholas, should we merge this, as well?

@TomNicholas TomNicholas merged commit 3ddd44f into xarray-contrib:main Feb 10, 2023
@keewis keewis deleted the copy-node branch February 10, 2023 15:20
flamingbear pushed a commit to flamingbear/rewritten-datatree that referenced this pull request Jan 19, 2024
…atree#201

* use relative paths for the copied descendants

* check that copying subtrees works

* changelog
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants