pydata / pydata/xarray

A constructor like DataTree.from_dict for data variables and coordinates

Open
#9,486 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

API design topic-DataTree
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

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

Contributor guide

Open the contributing guide

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.

Research direction

Start by reading the existing DataTree.from_dict implementation and the Dataset constructor behavior it is intended to mirror. Compare the proposed data and coords mappings, including fully qualified paths and supported DataArrayLike values; done means a settled constructor API with coverage for DataArray and Variable inputs at the requested tree locations.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.