Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Add a .to_node method upstream #191

Closed
TomNicholas opened this issue Jan 9, 2023 · 4 comments
Closed

Add a .to_node method upstream #191

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

Comments

@TomNicholas
Copy link
Member

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
Member 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
Member 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!

@TomNicholas
Copy link
Member Author

I'm not sure this would really be that helpful.

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

No branches or pull requests

2 participants