简体   繁体   中英

Create an xarray.DataArray where all the values satisfy a condition

I have an DataArray with 1 dimension, with each value corresponding to an ID. I'm trying to create a new array in the same format which contains the values and IDs for any cases where the values was beyond a limit

I have tried using xr.DataArray.where(condition,other) but this produces an array of the same size with 'nan' for cases where the condition was not met.

#create mock array
ID = np.arange(10)
values = np.random.uniform(0.5, 20, 10)
xr.DataArray(values, dims='ID', coords={'ID':ID})

xlim = 10

An example output could be an array with 5 elements (all whose values are greater than 10, our set limit) and their corresponding ID from the original array.

You can just use straight numpty indexing.

#create mock array
ID = np.arange(10)
values = np.random.uniform(0.5, 20, 10)
arr = xr.DataArray(values, dims='ID', coords={'ID':ID})

xlim = 10

arr[np.where(arr>xlim)]

<xarray.DataArray (ID: 4)> array([18.830967, 14.651416, 17.545492,
17.627902]) Coordinates:   * ID       (ID) int64 2 7 8 9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM