Xarray: slice coordinates with no dimensions

Update 2020-04-30

If you want to select data based on lat and lon, you could use where() to do something like:

data.where((data.lats > 25) & (data.lats < 30) & (data.lons > -80) & (data.lons < -75))

You could add drop=True to return a smaller-sized dataset instead of filling the non-matching values with NA.


Original answer

In your first example, you are not indexing by lat/lon but by each x and y's numeric index. That is, you are slicing between the 25th and 30th y and -80th and -75th x value. This explains why the lat/lon values don't make sense in your output.

You can associate your coordinates with dimensions by using xr.Dataset.set_index() like so:

data = data.set_index(y='lats')
data = data.set_index(x='lons')

Tags:

Python

Xarray