Skip to content

A constructor like DataTree.from_dict for data variables and coordinates #9486

Open
@shoyer

Description

@shoyer

What is your issue?

DataTree.from_dict supports DataTree and Dataset values.

It would be nice to also support DataArray and Variable objects with fully qualified paths. Similar to the Dataset constructor, we would need a separate coords argument.

I am thinking something like:

tree = DataTree.from_dict(
    data_vars={'/a': a, '/child/b': b, '/child/grandchild/c': c},
    coords={'/child/x': x},
)

where the values a, b, c and x are all DataArray objects (or anything supported in the Dataset constructor).

We could support this inside DataTree.from_dict by adding a new coords argument and renaming the first argument to data, i.e.,

    @classmethod
    def from_dict(
        cls,
        data: Mapping[str, DataArrayLike | Dataset | DataTree | None] | None = None,
        coords: Mapping[str, DataArrayLike] | None = None
        name: str | None = None,
    ) -> DataTree:

where DataArrayLike is an alias for everything valid in the Dataset constructor (DataArray | Variable | tuple | Numeric).

We could also make a dedicated constructor for this, e.g., DataTree.from_array_dict. If we took this approach, I would consider renaming the existing from_dict constructor, e.g., to DataTree.from_dataset_dict.

CC the usual DataTree crew: @TomNicholas @eni-awowale @flamingbear @owenlittlejohns

Metadata

Metadata

Assignees

No one assigned

    Labels

    API designtopic-DataTreeRelated to the implementation of a DataTree class

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions