Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
44 changes: 44 additions & 0 deletions xarray/core/datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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 :)

"""
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
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

If `parents` is `True`, create the additional paths needed to reach the
destination. Otherwise, raise an error. Set to `False` by default.

Returns
-------
DataTree
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DataTree
DataTree
Copied subtree with the node moved.


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,
Expand Down
35 changes: 35 additions & 0 deletions xarray/tests/test_datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,41 @@ def test_assign(self):
result = dt.assign({"foo": xr.DataArray(0), "a": DataTree()})
assert_equal(result, expected)

def test_move(self):
dt = DataTree.from_dict(
{"/to_move/A": None, "/to_move/B": None, "/destination/other": None}
)
result = dt.move("to_move", "destination")
expected = DataTree.from_dict(
{
"/destination/to_move/A": None,
"/destination/to_move/B": None,
"/destination/other": None,
}
)
assert_equal(result, expected)

def test_move_no_parents(self):
dt = DataTree.from_dict(
{"/to_move/A": None, "/to_move/B": None, "/destination/other": None}
)
with pytest.raises(KeyError, match="Could not reach node"):
dt.move("to_move", "destination/deep/down")

def test_move_parents(self):
dt = DataTree.from_dict(
{"/to_move/A": None, "/to_move/B": None, "/destination/other": None}
)
result = dt.move("to_move", "destination/deep/down", parents=True)
expected = DataTree.from_dict(
{
"/destination/deep/down/to_move/A": None,
"/destination/deep/down/to_move/B": None,
"/destination/other": None,
}
)
assert_equal(result, expected)


class TestPipe:
def test_noop(self, create_test_datatree):
Expand Down
Loading