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
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 @keewis ! I have one comment about possible unintended behavior here.
if not isinstance(root_data, cls): | ||
obj = cls(name=name, data=root_data, parent=None, children=None) | ||
else: | ||
obj = root_data.copy() |
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 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.
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.
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)
DataTree
objects as values infrom_dict
#159pre-commit run --all-files
docs/source/whats-new.rst