-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add DataTree.move to move a node to another place #9442
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -1060,6 +1060,50 @@ def drop_nodes( | |||||||
result._replace_node(children=children_to_keep) | ||||||||
return result | ||||||||
|
||||||||
def move(self, origin: str, destination: str, parents: bool = False) -> DataTree: | ||||||||
""" | ||||||||
Move a node to a different location in the tree. | ||||||||
|
||||||||
Parameters | ||||||||
---------- | ||||||||
origin: str | ||||||||
The node to move. | ||||||||
destination: str | ||||||||
The new node destination. | ||||||||
parents: bool, optional | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this argument should be renamed, though I'm not sure what to. Perhaps There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure either, I took inspiration from pathlib: https://docs.python.org/3/library/pathlib.html#pathlib.Path.mkdir There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I see. Taking inspiration from pathlib was a good idea. But I don't think it's a great name in this case... The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, good point. I will add an |
||||||||
If `parents` is `True`, create the additional paths needed to reach the | ||||||||
destination. Otherwise, raise an error. Set to `False` by default. | ||||||||
|
||||||||
Returns | ||||||||
------- | ||||||||
DataTree | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
Examples | ||||||||
-------- | ||||||||
>>> dt = DataTree.from_dict( | ||||||||
... { | ||||||||
... "/to_move/A": None, | ||||||||
... "/to_move/B": None, | ||||||||
... "/destination/other": None, | ||||||||
... } | ||||||||
... ) | ||||||||
>>> dt.move("to_move", "destination") | ||||||||
<xarray.DataTree> | ||||||||
Group: / | ||||||||
└── Group: /destination | ||||||||
├── Group: /destination/other | ||||||||
└── Group: /destination/to_move | ||||||||
├── Group: /destination/to_move/A | ||||||||
└── Group: /destination/to_move/B | ||||||||
""" | ||||||||
result = self.copy() | ||||||||
to_move = result[origin] | ||||||||
to_move.orphan() | ||||||||
result._set_item( | ||||||||
f"{destination}/{to_move.name}", to_move, new_nodes_along_path=parents | ||||||||
) | ||||||||
return result | ||||||||
|
||||||||
@classmethod | ||||||||
def from_dict( | ||||||||
cls, | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's another possible way of writing this method:
dt.move(destination)
where it's assumed that theorigin
is the current node, and the returned result is the root of the new tree.But I think your way is probably the less surprising of the two.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Should we do both, or would that be too confusing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think your way is fine! I just wanted to comment to point this out for posterity :)