-
I have created a dummy dataset below, in which I would like the "ti" dimension to have coordinates which depend on the selected "regime". It seems the way I define the DataArray actually accomplishes this (as evidenced when I print the ti values after selecting "free" regime, I notice that it returns only those specific to this regime. However, the line which is commented out fails due to no index being assigned to "ti". Is there a way I can set up this index correctly so that I could select a regime and ti value at once? I could imagine that maybe this isn't possible (because it begs the question: which index should we use when you don't do a selection specific to regime?). I thought also to perhaps create separate data arrays and combine into a dataset, but this has undesired behavior for my application, because when I do a selection that is specific to a regime, I notice that all ti values across all regimes are present. This is undesired, because in the actual real-world application for example:
Am I basically stuck with three different DataArrays, one for each regime? Or can someone help me fix this indexing problem?
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
If you don't mind a couple of extra steps, one simple way to achieve your desired behavior is: (
all_one_array
.sel(regime="free")
.set_xindex("ti")
.sel(ti=0.013, method="nearest")
) The Alternatively, it is possible to create a custom Xarray Index to be associated directly with both the (
all_one_array
.set_xindex(["regime", "ti"], CustomIndexSubclass)
.sel(regime="free", ti=0.013)
) Where the Unfortunately Xarray custom indexes are not much documented yet, apart from a rudimentary guide and some examples gathered here or scattered elsewhere. Hopefully this will improve in the near future. |
Beta Was this translation helpful? Give feedback.
If you don't mind a couple of extra steps, one simple way to achieve your desired behavior is:
The
.set_xindex()
step allows to build a (pandas) index from theti
coordinate once it has been reduced to a 1-dimension coordinate, which in turn allows using it with.sel()
.Alternatively, it is possible to create a custom Xarray Index to be associated directly with both the
regime
andti
coordinates:Where the
regime
andti
label values passed to.sel()
will be handled together b…