Skip to content

How to best structure data where one coordinate's values vary with another dimension? #10244

Answered by benbovy
mr483 asked this question in Q&A
Discussion options

You must be logged in to vote

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 .set_xindex() step allows to build a (pandas) index from the ti 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 and ti coordinates:

(
    all_one_array
    .set_xindex(["regime", "ti"], CustomIndexSubclass)
    .sel(regime="free", ti=0.013)
)

Where the regime and ti label values passed to .sel() will be handled together b…

Replies: 1 comment 1 reply

Comment options

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

Answer selected by mr483
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