I have a fairly large xr.Dataset with about 20 Data variables. I am only interested in keeping two of them. I see xr.Dataset.drop with drop variables for a Dataset.
I'm looking for syntax to keep variables. I tried f['hs','t01'] and got
Traceback (most recent call last): File "/nethome/rxb826/local/bin/miniconda3/lib/python3.6/site-packages/xarray/core/dataset.py", line 662, in _construct_dataarray variable = self._variables[name] KeyError: ('hs', 't01')
If there isn't simply syntax I could have a look at developing xr.Dataset.keep
I can also clunky manually create the Dataset as
ds = xr.Dataset({'hs': f['hs'], 't01': f['t01']})
You can use your getitem syntax using a iterable of variable names:
f_with_two_vars = f[['hs','t01']]
(See the xarray manual section on Indexing and selecting data for a more detailed explanation.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With