`DataTree.map_over_datasets` behavior can be obscure sometimes
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
- Issue type
- Documentation
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- documentation
Research direction
Start with the DataTree.map_over_datasets documentation linked in the issue and compare its description of func with the DatasetView behavior in xarray/core/datatree.py. Clarify that the callback receives a read-only view and explain when calling .copy() is needed for operations such as interpolate_na. Done means the docs make the behavior and workaround clear to readers.
Written by the indexing model from the issue text.
Description
What is your issue?
Consider this code:
import numpy as np
import pandas as pd
import xarray as xr
da = xr.DataArray(
data=[1., np.nan, 3.],
coords={"datetime": pd.date_range("2026-10-26", periods=3, freq="D")},
name="up",
)
dtree = xr.DataTree(dataset=da.to_dataset())
dtree.map_over_datasets(lambda ds: ds.interpolate_na(dim="datetime"))
AttributeError: DatasetView objects are not to be initialized directly.
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:
https://docs.xarray.dev/en/latest/generated/xarray.DataTree.map_over_datasets.html
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:
raise AttributeError(
"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?
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 1h
- Merged PRs (30d)
- 131
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pydata/xarray
-
Comparing attrs that contain a dict of NumPy arrays raises ValueError in identical / assert_identicalPossibly taken @SohamXYZDev claimed this 4 days ago. Openbug needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
Maintainers usually reply within 1 day
-
RangeIndex` with datetime64 bounds cannot be printed (`Unknown format code 'g'`)Possibly taken @d-chambers claimed this 35 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
pydata/xarray#11598 · 3 comments ·
Maintainers usually reply within 1 day
-
DataTree constructor error message says dict even though dict is not acceptedPossibly taken @aryansk claimed this 75 days ago. Openbug contrib-good-first-issue topic-DataTree topic-error reporting
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
pydata/xarray#11514 · 1 comment ·
Maintainers usually reply within 1 day
-
Use `zarr.Array.async_array`Possibly taken @0xrutabaga claimed this 20 days ago. Opentopic-zarr
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
DOC: Add new backend page does not mention DataTreePossibly taken A pull request linked to this issue is open or already merged. Openneeds triage
Difficulty 1/5 Under an hour Newbie friendliness 65/100
pydata/xarray#10800 · 1 comment ·
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
scikit-learn-contrib/category_encoders#553 ·
Maintainers usually reply within 1 day
-
tests/strategies_test.py:174 goes uncovered on about 1 run in 100, failing the coverage ratchetOpenflaky-watch
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
btclib-org/btclib#2839 ·
Maintainers usually reply within 1 day
-
BarrelList constructor drops false-valued iterable contentsPossibly taken @Gambit-Checkmate claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 19 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
vllm-project/vllm#63863 ·
Maintainers usually reply within 1 day