Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python-xarray: xarray.Dataset.keep?

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']})

like image 294
Ray Bell Avatar asked Jul 12 '26 18:07

Ray Bell


1 Answers

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.)

like image 190
jhamman Avatar answered Jul 18 '26 14:07

jhamman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!