Skip to content

Equivalent of np.take for xarray DataArray #7308

Answered by TomNicholas
peendebak asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, it's just .isel().

From the numpy docs:

A call such as np.take(arr, indices, axis=3) is equivalent to arr[:,:,:,indices,...].

In xarray we use dimension names in place of axis numbers, so if if you have a DataArray da with a dimension named time that corresponds to your axis=2, you could achieve the same thing with

da.isel(time=2)

Or in your example, let's say we have created a dataarray with data X and 5 dimensions:

da_X = xr.DataArray(data=X, dims=["a", "b", "c", "d", "e"])

The equivalent isel call would be

index=1
da_X.isel(c=index)

because dimension "c" now corresponds to axis number 2 of your data.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@peendebak
Comment options

Answer selected by peendebak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants