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

Add a .to_node method upstream #191

Open
TomNicholas opened this issue Jan 9, 2023 · 3 comments
Open

Add a .to_node method upstream #191

TomNicholas opened this issue Jan 9, 2023 · 3 comments
Labels
enhancement New feature or request Upstream issue

Comments

@TomNicholas
Copy link
Collaborator

We should add a to_node method to both dataset and DataArray (would require a name) upstream, as another way of creating a single node DataTree.

@TomNicholas
Copy link
Collaborator Author

pydata/xarray#7418 would close this by adding a .to_datatree method on both Dataset and DataArray.

@utkarsh-chaurasia
Copy link

Hello, I am an Outreachy participant and I would like to contribute to this @TomNicholas's question.

We should add a to_node method to both dataset and DataArray (would require a name) upstream, as another way of creating a single node DataTree.

Adding a to_node method to both Dataset and DataArray classes could be a useful addition to enable creating single node DataTree objects directly from these classes.

Assuming that you are using the xarray library, here's an example implementation of the to_node method for both Dataset and DataArray classes:

Python code :

import datatree
import xarray as xr

def to_node(self, name):
    return datatree.DataTree({
        name: self.to_dataset()
    })

xr.Dataset.to_node = to_node
xr.DataArray.to_node = to_node

This implementation defines a to_node method that takes a name argument and returns a single node DataTree object with the given name, containing the Dataset or DataArray object as its value.

With this implementation, you can now create a single nodeDataTreeobject directly from a Dataset or DataArray object as follows:

Python code:

ds = xr.Dataset({'foo': [1, 2, 3]})
tree = ds.to_node('my_node')

This will create a single node DataTree object named my_node with the Dataset object ds as its value.

Similarly, you can create a single node DataTree object from a DataArray object as follows:

Python code :

da = xr.DataArray([1, 2, 3])
tree = da.to_node('my_node')

This will create a single node DataTree object named my_node with the DataArray object da as its value.

@TomNicholas TomNicholas removed the good first issue Good for newcomers label Mar 27, 2023
@TomNicholas
Copy link
Collaborator Author

Hi @utkarsh-chaurasia , thanks for your interest.

Your suggestion would work as an implementation of .to_node, nice.

Normally we would then ask that you submit a pull request adding your implementation to the codebase. However in this case the issue requires changing xarray upstream (rather than changing xarray-datatree's code here). Because of that it might just be easier to wait until #7418 is merged.

Please look for other issues that you find interesting though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Upstream issue
Projects
None yet
Development

No branches or pull requests

2 participants