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

drop all but specified data_variables/coordinates as a convenience function #2884

Closed
jbusecke opened this issue Apr 10, 2019 · 5 comments · Fixed by #2894
Closed

drop all but specified data_variables/coordinates as a convenience function #2884

jbusecke opened this issue Apr 10, 2019 · 5 comments · Fixed by #2894

Comments

@jbusecke
Copy link
Contributor

I often work with datasets that consist out of a lot of data_variables and coordinates.
Often I am only concerned about a subset of variables, and for convenience drop all but a selected list of variables with a little snippet like this:

Code Sample, a copy-pastable example if possible

def xr_keep(obj, varlist):
    """drop all data_vars exept the ones provided in `varlist` """
    obj = obj.copy()
    drop_vars = [a for a in obj.data_vars if a not in varlist]
    return obj.drop(drop_vars)

I would love to have this functionality available as a DataArray/Dataset function. It could look something like da_slim = da.drop_all_but(['var1, 'var3']). Would this be of interest to people here? Then I could try to put in a PR.

@spencerkclark
Copy link
Member

Something that is currently possible, though perhaps not super well-documented, is ds.get(['var1', 'var3']). This will select only 'var1' and 'var3' from the Dataset and omit any others.

@dcherian
Copy link
Contributor

ds[['var1', 'var3']] also works AFAIR.

@jbusecke Can you send in a PR to update the docs? https://xarray.pydata.org/en/stable/data-structures.html#dataset

@dcherian
Copy link
Contributor

dcherian commented Apr 10, 2019

I often work with datasets that consist out of a lot of data_variables and coordinates.

I'm pretty sure there's an issue request somewhere to add keep_vars to the open_*dataset functions if you wanted to tackle that too :)

@jbusecke
Copy link
Contributor Author

Wow. Thats awesome. Had no clue about it. I will put in a PR for the docs for sure. Might take a bit though.
Ill also take a look at the keep_vars option.

@jbusecke
Copy link
Contributor Author

Ok I have submitted a PR for the xarray.Dataset.get function.

@dcherian, I was not able to find that issue you mentioned. I would certainly be interested to have a look in the future.

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

Successfully merging a pull request may close this issue.

3 participants