Skip to content

Add inherit='all' option to DataTree.to_dataset()#11230

Open
aladinor wants to merge 6 commits intopydata:mainfrom
aladinor:inherit-all-coords
Open

Add inherit='all' option to DataTree.to_dataset()#11230
aladinor wants to merge 6 commits intopydata:mainfrom
aladinor:inherit-all-coords

Conversation

@aladinor
Copy link
Contributor

Summary

Adds inherit='all' option to DataTree.to_dataset() so users can inherit all parent coordinates, not just indexed ones.

Currently, DataTree only inherits indexed (dimension) coordinates from parent nodes. Non-index coordinates are silently dropped during inheritance. This PR extends the inherit parameter from bool to bool | Literal["all", "indexes"]:

  • True / "indexes" — inherit only indexed coordinates (default, backward compatible)
  • "all" — inherit all coordinates, including non-index coordinates
  • False — node-only coordinates

Motivation: inheriting non-index coordinates

As described in #10812, non-index coordinates defined on a parent node are not inherited by child nodes. This is a common need in projects like xradar, where non-index coordinates such as latitude, longitude, and altitude are stored on the root node and need to be accessible on child sweep nodes:

import numpy as np
import xarray as xr

root = xr.Dataset(
    {"volume_number": ("time", [1, 2])},
    coords={
        "time": np.array(["2026-01-01", "2026-01-02"], dtype="datetime64[ns]"),
        "latitude": 36.74,
        "longitude": -98.13,
        "altitude": 378.0,
    },
)

child = xr.Dataset(
    {"temperature": (["time", "x"], np.random.rand(2, 3))},
    coords={"x": [0, 1, 2]},
)

dt = xr.DataTree.from_dict({"/": root, "sweep_0": child})

# Before: latitude, longitude, altitude are not inherited
dt["sweep_0"].to_dataset(inherit=True)

# After: all parent coords including non-index ones are available
dt["sweep_0"].to_dataset(inherit="all")

See also: openradar/xradar#337

Implementation

  • Added _coord_variables_all property on DataTree (like _coord_variables but inherits all parent coords, not just indexed ones)
  • Added _resolve_inherit() helper to centralize inherit parameter validation and dispatch
  • Updated to_dataset() and _to_dataset_view() to use the new helper

cc @kmuehlbauer

@github-actions github-actions bot added the topic-DataTree Related to the implementation of a DataTree class label Mar 13, 2026
@aladinor aladinor changed the title Add inherit='all' option to DataTree.to_dataset() Add inherit='all' option to DataTree.to_dataset() Mar 14, 2026
Copy link
Contributor

@kmuehlbauer kmuehlbauer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aladinor Thanks, this is LGTM! Would be great if participants of #10812 could chime in on the implementation here.

cc @shoyer @TomNicholas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic-DataTree Related to the implementation of a DataTree class

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Datatree does not inherit non-dimension coordinates

2 participants