xarray icon indicating copy to clipboard operation
xarray copied to clipboard

Add DataTree.move to move a node to another place

Open Armavica opened this issue 1 year ago • 2 comments

  • [x] Closes https://github.com/xarray-contrib/datatree/issues/210
  • [x] Tests added
  • [ ] User visible changes (including notable bug fixes) are documented in whats-new.rst
  • [ ] New functions/methods are listed in api.rst

Armavica avatar Sep 07 '24 16:09 Armavica

There are actually several non-trivial design questions to answer for this feature. The main one I see is the semantics of the origin and destination paths. What do we want to happen when we do dt.move("/to_move", "/other/path") for the following DataTree:

dt = DataTree.from_dict({"/to_move/child": None, "other/path/here": None})
<xarray.DataTree>
Group: /
├── Group: /to_move
│   └── Group: /to_move/child
└── Group: /other
    └── Group: /other/path
        └── Group: /other/path/here

Option 1: destination is the new name of origin

dt.move("/to_move", "/other/path")
<xarray.DataTree>
Group: /
└── Group: /other
    └── Group: /other/path
        └── Group: /other/path/child

With this option, if destination existed before the move, it is overwritten by origin.

Option 2: destination is the new root of origin

dt.move("/to_move", "/other/path")
<xarray.DataTree>
Group: /
└── Group: /other
    └── Group: /other/path
        ├── Group: /other/path/here
        └── Group: /other/path/to_move
            └── Group: /other/path/to_move/child

With this option, if destination existed before the move, it is not overwritten, but one of its children might if it has the same name as origin

Option 3: if destination exists it is the new root, otherwise it is the new name

This seems closer to the unix mv semantics.

Armavica avatar Sep 10 '24 22:09 Armavica

With the addition of copy, copy_into, move and move_into into pathlib as of Python 3.14, I am wondering if we shouldn't take inspiration from this API

Armavica avatar Oct 21 '25 13:10 Armavica