-
-
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?
Conversation
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.
Thanks so much @Armavica !
|
||
Returns | ||
------- | ||
DataTree |
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.
DataTree | |
DataTree | |
Copied subtree with the node moved. |
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 comment
The 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 create_intermediates
?
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 am not sure either, I took inspiration from pathlib: https://docs.python.org/3/library/pathlib.html#pathlib.Path.mkdir
Happy to change to anything.
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.
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 exists_ok
arg in pathlib.Path.mkdir
is also potentially relevant - the user might want to avoid replacing the subtree already at that path.
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.
Ah, good point. I will add an exists_ok
argument for now, until we settle on names for these arguments.
@@ -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: |
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 the origin
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 :)
There are actually several non-trivial design questions to answer for this feature. dt = DataTree.from_dict({"/to_move/child": None, "other/path/here": None})
Option 1:
|
whats-new.rst
api.rst