pydata / xarray

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

DataTree.coords.__setitem__ is broken #9204

Closed shoyer closed 3 weeks ago

shoyer commented 3 months ago

One messy thing is that it appears that assignment via .coords is broken on DataTree even at main.

Ah yes - I forgot there's a TODO deep somewhere for that 😅 I left it for later because it seemed like it might require changing the DatasetCoordinates class that ds.coords returns. (Surely that class could just be replaced by the new xr.Coordinates class now??)

Originally posted by @TomNicholas in https://github.com/pydata/xarray/issues/9063#issuecomment-2189180572

TomNicholas commented 3 weeks ago

I'm trying to have a go at this but it's made harder by #9203 not being implemented. The problem is that the DatasetCoordinates class works by storing the Dataset object as ._data, then altering it in-place by altering ._data._variables and ._data._coord_names. But the same approach doesn't work for DataTree because that uses ._data_variables and ._coord_variables internally, so you can't get away with just passing the DataTree object to the DatasetCoordinates constructor, even though conceptually that should work.

So to make it work right now requires a new DataTreeCoordinates object. That's fine, but if #9203 were completed then we wouldn't really need that new class.