xarray-contrib / datatree

WIP implementation of a tree-like hierarchical data structure for xarray.
https://xarray-datatree.readthedocs.io
Apache License 2.0
162 stars 43 forks source link

Attributes get dropped on empty parent nodes #278

Closed slevang closed 7 months ago

slevang commented 7 months ago

When applying a function to a node that has attrs but no data:

dt = DataTree()
dt.attrs = {"foo": "bar"}
ds = xr.Dataset({"x": xr.DataArray(0)})
dt["y"] = DataTree(data=xr.DataArray(0, name="y"))

DataTree('None', parent=None)
│   Dimensions:  ()
│   Data variables:
│       *empty*
│   Attributes:
│       foo:      bar
└── DataTree('y')
        Dimensions:  ()
        Data variables:
            y        int64 0

map_over_subtree drops the attributes:

dt.attrs
{'foo': 'bar'}

dt.compute().attrs
{}

Related: #262