pydata / xarray

N-D labeled arrays and datasets in Python
https://xarray.dev
Apache License 2.0
3.56k stars 1.07k forks source link

DataTree: Copy during assignment to child nodes? #9489

Open TomNicholas opened 5 days ago

TomNicholas commented 5 days ago

I think both of these should have the same behavior:

bart = xr.DataTree(name="Bart")
lisa = xr.DataTree(name="Lisa")
homer = xr.DataTree(name="Homer", children={"Bart": bart, "Lisa": lisa})
print(list(lisa.siblings))

homer2 = xr.DataTree(name="Homer")
homer2.children = {"Bart": bart, "Lisa": lisa}
print(list(lisa.siblings))
[]
['Bart']

@TomNicholas

Originally posted by @flamingbear in https://github.com/pydata/xarray/issues/9033#issuecomment-2346526772

TomNicholas commented 5 days ago

I'm pretty sure I know what's going on here: we currently copy inputs to the constructor but not to the .children setter. This is the assignment version of https://github.com/pydata/xarray/issues/9196 - calling a method on homer2 has the side-effect of also altering one of the inputs lisa.

This could probably be fixed as part of #9477.

flamingbear commented 5 days ago

Let's be sure to include this doc update when this is complete either in #9477 or a doc PR update