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

allow DataTree objects as root node in from_dict #221

Open
wants to merge 4 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion datatree/datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,10 @@ def from_dict(

# First create the root node
root_data = d.pop("/", None)
obj = cls(name=name, data=root_data, parent=None, children=None)
if not isinstance(root_data, cls):
obj = cls(name=name, data=root_data, parent=None, children=None)
else:
obj = root_data.copy()
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think there is potential for bugs here - .copy() will copy child nodes too, so if root_data has children already they will presumably be copied into the new tree. You could also then double-specify the contents of a single node, once as the child of the root and again in the supplied dict.

In fact your previous PR might do this too - I think we should decide what we want the behavior to be: either drop all children of supplied nodes or explicitly test that we keep children too.

If we do want to drop the children it would probably be clearer to just create a new node using a copy of the supplied node's name and data.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is a bit similar to what xarray does already when passing DataArray objects with coordinates to the Dataset constructor / Dataset.assign*: those coordinates will be carried over (unintentionally overriding coordinates explicitly passed in the same call).

I'm not sure if we need to copy the behavior, though.

I didn't have a need for it ever since opening this PR, so this is also pretty low-priority for me (as you might have guessed from the long delay)


if d:
# Populate tree with children determined from data_objects mapping
Expand Down
7 changes: 7 additions & 0 deletions datatree/tests/test_datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,13 @@ def test_datatree_values(self):

dtt.assert_identical(actual, expected)

def test_datatree_values_root(self):
expected = DataTree(data=xr.Dataset({"a": 1}))

actual = DataTree.from_dict({"/": expected})

dtt.assert_identical(actual, expected)

def test_roundtrip(self, simple_datatree):
dt = simple_datatree
roundtrip = DataTree.from_dict(dt.to_dict())
Expand Down
2 changes: 2 additions & 0 deletions docs/source/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Bug fixes
By `Tom Nicholas <https://github.com/TomNicholas>`_.
- copy subtrees without creating ancestor nodes (:pull:`201`)
By `Justus Magin <https://github.com/keewis>`_.
- allow `DataTree` objects as root node in `from_dict` (:pull:`221`)
By `Justus Magin <https://github.com/keewis>`_.

Documentation
~~~~~~~~~~~~~
Expand Down