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

Support for xarray #200

Closed
juanfcocontreras opened this issue May 22, 2020 · 12 comments
Closed

Support for xarray #200

juanfcocontreras opened this issue May 22, 2020 · 12 comments

Comments

@juanfcocontreras
Copy link

juanfcocontreras commented May 22, 2020

This is a feature request: add support for xarray (https://xarray.pydata.org).

xarray is a package strongly inspired by pandas that adds multi-dimensional arrays (among other things). Once a dimension or time is selected, data can be converted to dataframe directly for dtale display (https://xarray.pydata.org/en/stable/generated/xarray.DataArray.to_dataframe.html)

Recently xarray added the possibility to preview the metadata (dimensions and coordinates) in jupyter:

pydata/xarray#3425

The idea could be to try something similar, and by selecting a certain dimension or time, display the data in dtale.

I know this might not be a trivial request, so I'm offering to help implement it. However, I think it's worth thinking about because xarray is a package that is becoming very popular in the scientific community.

A first approach might be:

If you want to show all the dimensions at one point in time:

data.sel(time="selected_time").to_dataframe()

If you want to show all your time data from a particular dimension:

data["dimension_name"].to_dataframe()

@aschonfeld
Copy link
Collaborator

@juanfcocontreras Sorry, was finishing up a couple other enhancements. Will start working on this one in this release :)

@aschonfeld
Copy link
Collaborator

@juanfcocontreras so how about being able to pass in an xarray to dtale.show and what it will do is keep a reference to the xr.Dataset you've passed in and by default display the entire dataframe (ds.to_dataframe()), but will also present the user with another option viewing each individual dataframe based on different dimension combinations:

  • ds.sel(col1=val1, col2=val2, ... colN=valN).to_dataframe()

I have noticed that if you call to_dataframe on the output of a .sel() call when there's zero dimenstions yields the following exception: ValueError: no valid index for a 0-dimensional object

Rather than throw an exception I'll call .to_dict() instead and convert that to a dataframe with this snippet.

data = {}
for coord, coord_data in x['coords'].items():
    data[coord] = coord_data['data']
for col, col_data in x['data_vars'].items():
    data[col] = col_data['data']
pd.DataFrame([data])

Sound good?

@juanfcocontreras
Copy link
Author

That sounds awesome! I can't wait to try it!

Thank you very much!

@aschonfeld
Copy link
Collaborator

@juanfcocontreras Here's what I have so far: https://youtu.be/eDg4nWGjw9k

I might try and add a reverse option where you can convert a dataframe you're viewing to an xarray dataset as well. Then you can view different dimensions. I still need to give users the ability to open a dimension in a new slice. That's not hard to add though. :)

@juanfcocontreras
Copy link
Author

Wow, that's impressive! 👏 👏

@aschonfeld
Copy link
Collaborator

@juanfcocontreras just released v1.8.16 with this functionality added. Here's the docs

@juanfcocontreras
Copy link
Author

Thank you very much. Do you know when it will be available on conda-forge? Your releases are usually almost instantaneous, but this time it's taking longer than usual.

@aschonfeld
Copy link
Collaborator

Should be there now

@juanfcocontreras
Copy link
Author

I still don't see it, I guess it'll take a while to update:

https://anaconda.org/conda-forge/dtale

Thanks anyway!

@aschonfeld
Copy link
Collaborator

Ahhh, now i see. It looks like the conda build broke. I’ll fix it tonight. Apologies

@aschonfeld aschonfeld reopened this Jun 8, 2020
@aschonfeld
Copy link
Collaborator

Alrighty, @juanfcocontreras, should be all set now. Let me know and I'll close this. Apologies for the wait.

@juanfcocontreras
Copy link
Author

Installed and willing to try it! I'll give you feedback!

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants