Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

`DataTree.map_over_datasets` behavior can be obscure sometimes

Open Beginner friendly
#11,813 0 comments 0 reactions 0 assignees View on GitHub

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
Tech stack
numpy, pandas, python
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

needs triage
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

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pydata/xarray

All issues in pydata/xarray

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.