Stream: python-questions
Topic: indexing along 1 dimension with different values
Anna-Lena Deppenmeier (Jun 09 2021 at 18:32):
Hi all, I'm trying to index a dataarray
with dimensions time
and depth
with a varying depth index. So imagine my da
has dims time:5, z_t:5
, and I want to get the values for the [12,13,14,12,11]th z_t level per time step -- so time=0 at z_t=12, time=1 at z_t=13, time=2 at z_t=14 etc. If I just pass da.isel(z_t=[12,13,14,12,11])
it will give me all those indices per timestep. I've been reading the doc (https://xarray.pydata.org/en/stable/user-guide/indexing.html#vectorized-indexing) but so far no luck, can someone help?
Deepak Cherian (Jun 09 2021 at 18:56):
Oops I pointed you to the wrong place. Try https://xarray.pydata.org/en/stable/user-guide/indexing.html#more-advanced-indexing
Anna-Lena Deppenmeier (Jun 09 2021 at 19:11):
ok, this explains how to index these things (ix, iy) = ((0, 0), (1, 1), (6, 0))
, but I would like to index all x elements with varying y indices.
Anna-Lena Deppenmeier (Jun 09 2021 at 19:15):
It seems to allow me to set a new dimension for the indexed values, but I can't see how to specify that I want to index subsequently along time, and not extract all those indices for each time step.
Anna-Lena Deppenmeier (Jun 09 2021 at 19:45):
I think it's a problem with how I'm finding the index. In another example, I have found the index of the EUC maximum
euc_idz_max = (ds_uvwjq_months.UVEL
.isel(nlat=lola_inds['j_0n'],
nlon=lola_inds['i_140_w']).argmax(dim='z_t')
.compute())
and then used euc_idz_max
to plot the actual velocity when it's maximum in the column.
euc_idz_max
looks like the attached first image.
pasted image
I am able to plot using this array (see next image).
pasted image
The index I am creating now is finding the level where the temperature is closest to 20C:
(np.abs(20-(t_5day_140W_test.isel(time=slice(0,10)))).argmin(dim=["z_t"]))
and this looks like the next image
pasted image
maybe that's why it's not working the same way? if I index my other array with this I get an error.
Deepak Cherian (Jun 09 2021 at 20:36):
Can you create a simple example of what you're trying to do? (your first three comments in this thread seem to contradict each other)
Anna-Lena Deppenmeier (Jun 09 2021 at 21:02):
hm. I don't think I can provide a simpler example, because what I'm trying to do works in one example and doesnt' work in another, and I don't know what the difference is and how to manipulate it so that it works on one case and not in another.
I can try to summarize and be clearer:
I am trying to find an index which is based on one quantity (e.g. EUC max, which is the example that works, or 20C isotherm, which is the example that does not work).
Then I would like to use that list of indices (list because it evolves in time) and pass it to .isel
so that it selects per time step the index (in both cases depth). For the EUC example this works, for the 20C example it doesn't. The difference I can see is that my EUC index looks different from my 20C index, and I'm thinking this might be the problem. But I don't know why it's different.
Anna-Lena Deppenmeier (Jun 09 2021 at 21:04):
Does this help?
Deepak Cherian (Jun 09 2021 at 21:16):
well the first one is a dataArray and the second one is a dictionary.
Deepak Cherian (Jun 09 2021 at 21:18):
because you provided dim='z_t'
in the first one, and dim=['z_t']
in the second one.
Anna-Lena Deppenmeier (Jun 09 2021 at 21:20):
That was it. Now it works. Thank you! I knew I was making some kind of silly mistake..
Last updated: Jan 30 2022 at 12:01 UTC