Maybe this functionality already exists in some way, but I haven't seen an obvious way to do it.
Frequently I want to retrieve a subset of a dataset where I don't know exactly the index. For example if I have two coordinates x and y, I want to provide conditions like x< 100 & x>3, y >=2. Some functions like this exist in the Dplyr package in r using the filter function (e.x. filter(ds, x>= 100 | x <-1). Is such a thing possible using a function in xarray or must I build the boolean index myself using something like ds[np.meshgrid(ds.x < 100 , ds.y>5)]?
Notice the desired functionality is a lot like xr.where except the conditions are on the coordinates and instead of returning a mask, the function should return a smaller dataframe, if possible.
ds = xr.Dataset({'foo': (('x', 'y'), np.random.rand(4, 4))},
coords={'x': [10, 20, 30, 40],
'y': [30, 40, 50, 60]})
ds.isel((ds.y>=30) & (ds.y <=50)) #doesn't work, desired functionality
xr.Dataset({'foo': (('x', 'y'), np.random.rand(4, 3))},
coords={'x': [10, 20, 30, 40],
'y': [30, 40, 50]})
Does the drop argument on the .where() method satisfy your needs? For example,
ds.where((ds.y >= 30) & (ds.y <= 50), drop=True)
reduces the shape of the foo variable to (4, 3), as desired.
Sounds like this is what I鈥檓 looking for, I鈥檒l try it out soon thank you! I wasn鈥檛 aware that where had a drop option
@jthielen is there a better place to ask these questions in the future? I like the the package quite a bit, but sometimes I struggle to answer basic questions like this from the documentation alone (ex: this page could maybe use a few words explaining the examples?). On the other hand I asked here because I wasn't sure how much traction my question would get on stack exchange.
take a look at https://xarray.pydata.org/en/stable/#get-in-touch where a few options are listed. I'm not a active user of SO / SE so I can't comment on that, but I think it is fine to ask questions like this one using the issue tracker.
Also, thanks for reaching out about the examples. I agree we should have descriptions on what each of them is trying to achieve, and we should also think about linking to the relevant section in the narrative documentation. In this case, that would be https://xarray.pydata.org/en/stable/indexing.html#masking-with-where
we could also add something here: https://xarray.pydata.org/en/stable/howdoi.html
Very helpful resources, thank you all!
Most helpful comment
take a look at https://xarray.pydata.org/en/stable/#get-in-touch where a few options are listed. I'm not a active user of SO / SE so I can't comment on that, but I think it is fine to ask questions like this one using the issue tracker.
Also, thanks for reaching out about the examples. I agree we should have descriptions on what each of them is trying to achieve, and we should also think about linking to the relevant section in the narrative documentation. In this case, that would be https://xarray.pydata.org/en/stable/indexing.html#masking-with-where