Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shoyer opened this issue Sep 12, 2024 · 0 comments
Open
Labels
API design topic-DataTree Related to the implementation of a DataTree class

Comments

@shoyer
Copy link
Member

shoyer commented Sep 12, 2024

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

@shoyer shoyer added API design topic-DataTree Related to the implementation of a DataTree class labels Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API design topic-DataTree Related to the implementation of a DataTree class
Projects
None yet
Development

No branches or pull requests

1 participant