You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DataTree.map_over_datasets behavior can be obscure sometimes #11813
This will not run because map_over_datasets will iterate over DatasetView objects, even though the documentation says explicitly that the parameter func expects Dataset:
But if you dig in xarray/core/datatree.py and have a look at DatasetView, this is stated in an exception that I didn't encounter:
def__setitem__(self, key, val) ->None:
raiseAttributeError(
"Mutation of the DatasetView is not allowed, please use `.__setitem__` on the wrapping DataTree node, ""or use `dt.to_dataset()` if you want a mutable dataset. If calling this from within `map_over_datasets`,""use `.copy()` first to get a mutable version of the input dataset."
)
So the solution is to run lambda ds: ds.copy().interpolate_na(dim="datetime"). Not intuitive at all, expecially if one has been using map_over_datasets with other functions without a problem. Shouldn't this be more clear in the documentation?
I hit the same thing on xarray 2026.9.0 (Python 3.13, Linux), and I think it's a bug more than a docs gap. interpolate_na doesn't mutate the input. It goes through _apply_over_vars_with_dim in xarray/core/missing.py, which builds the result with
Inside map_over_datasets, type(self) is DatasetView, and its __init__ always raises. Other methods I tried on a view (mean, fillna, differentiate) return a plain Dataset and work fine.
ffill, bfill, polyfit and curvefit fail the same way. The last two use the same pattern in xarray/computation/fit.py (type(obj)(...)). All of these are still there on main (aee3dc5).
As a quick check I monkeypatched _apply_over_vars_with_dim to build Dataset(coords=self.coords, attrs=self.attrs) instead, and interpolate_na and ffill then worked inside map_over_datasets. I haven't run the test suite with that change, so it's just a pointer. Until it's fixed, ds.copy() first is the workaround, as you found.
Activity
mohamd764 commentedon Oct 27, 2026
I hit the same thing on xarray 2026.9.0 (Python 3.13, Linux), and I think it's a bug more than a docs gap.
interpolate_nadoesn't mutate the input. It goes through_apply_over_vars_with_diminxarray/core/missing.py, which builds the result withInside
map_over_datasets,type(self)isDatasetView, and its__init__always raises. Other methods I tried on a view (mean,fillna,differentiate) return a plainDatasetand work fine.ffill,bfill,polyfitandcurvefitfail the same way. The last two use the same pattern inxarray/computation/fit.py(type(obj)(...)). All of these are still there on main (aee3dc5).As a quick check I monkeypatched
_apply_over_vars_with_dimto buildDataset(coords=self.coords, attrs=self.attrs)instead, andinterpolate_naandffillthen worked insidemap_over_datasets. I haven't run the test suite with that change, so it's just a pointer. Until it's fixed,ds.copy()first is the workaround, as you found.