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

Align datatree to_dict and from_dict to similar methods on Dataset and DataArray level #9080

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

etienneschalk
Copy link
Contributor

@etienneschalk etienneschalk commented Jun 8, 2024

This Pull Request adds two new methods on the DataTree class: to_native_dict and from_native_dict.

~~The existing ones are renamed to to_paths_dict and from_paths_dict, as their working data structure is a dict of paths to xarray data structures. ~~

  • DataTree.from_dict -> DataTree.from_paths_dict
  • DataTree.to_dict -> DataTree.to_paths_dict

The goal is to convert back-and-forth DataTrees to Python dictionaries, with the final goal (left to the user) of dumping these dictionaries to JSON, and read back JSON to a DataTree. The motive is exposed in #9074

Open Points


(1)

Since the DataTree.to_dict and DataTree.from_dict already exist on DataTree, but with a different behaviour, this change is a breaking change. I think it is needed for having a behaviour similar on the existing Dataset methods.

However, if the current usage of from_dict and to_dict is already so widespread that it cannot be renamed easily, then an option can be added to the from_dict and to_dict methods to switch between the current behaviour (convert a mapping of str paths to xarray objets to a DataTree) and the new (convert to a recursive dict with no left xarray structure, delegating lower-lever conversion of datasets to Dataset.to_dict and Dataset.from_dict


@etienneschalk etienneschalk force-pushed the eschalk/align-datatree-to_dict-to-existing branch from 57ad774 to c0a6558 Compare June 8, 2024 16:50
@etienneschalk etienneschalk marked this pull request as ready for review June 8, 2024 17:22
xarray/core/dataset.py Outdated Show resolved Hide resolved
@etienneschalk etienneschalk force-pushed the eschalk/align-datatree-to_dict-to-existing branch from 7173eac to b93b97e Compare June 10, 2024 20:25
@etienneschalk etienneschalk changed the title Allign datatree to_dict and from_dict to similar methods on Dataset and DataArray level Align datatree to_dict and from_dict to similar methods on Dataset and DataArray level Jun 10, 2024
@TomNicholas TomNicholas added the topic-DataTree Related to the implementation of a DataTree class label Jun 11, 2024
@etienneschalk etienneschalk force-pushed the eschalk/align-datatree-to_dict-to-existing branch from 2147def to a248296 Compare June 22, 2024 09:41
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: Align from_dict and to_dict behaviours to their Dataset equivalents
2 participants