Skip to content

.sel() unexpectedly forgets to slice the uxgrid too, for UxDataArray and UxDataset #1641

Closed

Description

@Sevans711

Version

2026.7.0

How did you install UXarray?

Source

What happened?

When calling .sel() on a UxDataArray or UxDataset, the underlying uxgrid does not get sliced at all.

An example workflow that might want to utilize .sel() instead of .isel() is if you are slicing multiple times in a row, or doing subsetting, and want to utilize faces' original labels. In that case, it would be reasonable to do something like:

arr = ux.tutorial.open_dataset("outCSne30-vortex")['psi']
arr1 = arr.assign_coords(n_face=np.arange(arr.n_face.size))   # make sure to track the faces' original labels
arr2 = arr1.isel(n_face=range(0, 100, 5))
arr3 = arr2 + 7   # or some other math or analysis operations
# then realize "oh I want to check what the results look like on what were originally faces 20, 30, and 40"
arr4 = arr3.sel(n_face=[20,30,40])
# and "I want to remind myself where the data is located so I want to plot this"
arr4.plot()

This currently works up until the last line, when there is a crash because the grid wasn't also sliced to just faces 20, 30, and 40.

What did you expect to happen?

I expected .sel() to work similarly to .isel(); the underlying grid should be sliced appropriately. Or, at the very least, I expected to receive a warning message or a crash to suggest that .sel() is not yet fully supported by uxarray, rather than a confusing error later on when the data no longer aligns with the grid (for example, the code above makes ValueError: Length of values (3) does not match length of index (20)).

Can you provide a MCVE to repoduce the bug?

arrA = ux.tutorial.open_dataset("outCSne30-vortex")['psi']
arrB = arrA.sel(n_face=[20,30,40])
arrB.uxgrid.n_face   # Got 5400; expected 3.

Activity

  1. added
    bug
    on Aug 3, 2026
  2. Sevans711 commented on Aug 5, 2026

    @Sevans711
    CollaboratorAuthor

    Oh! I didn't realize it originally, but this is almost certainly related to #1444! @dylannelson @erogluorhan. This would easily explain how the Grid's time and the data's time ended up being different times, because the example there used sel!

  3. moved this from 📚 Backlog to 📝 To-Do in UXarray Developmenton Aug 13, 2026
  4. moved this from 📝 To-Do to 🏗 In progress in UXarray Developmenton Aug 18, 2026
  5. self-assigned this
    on Aug 19, 2026
  6. rajeeja commented on Aug 19, 2026

    @rajeeja
    Contributor

    @erogluorhan @Sevans711 Agreed, and the framing I'd use is that neither case is a UXarray invention: xarray already treats sel on a dim with no coordinate as positional, so case A is just xarray semantics and case B is xarray's label lookup. So implement both by resolving labels to positions and forwarding to isel, keeping Grid.isel as the single place that rebuilds topology — uxgrid._ds.sel(n_face=7) would slice only the variables carrying n_face without renumbering face_node_connectivity or dropping orphaned nodes. The rule to agree on: sel is xarray's sel plus the grid comes along, with no other divergence.

  7. Sevans711 commented on Aug 19, 2026

    @Sevans711
    CollaboratorAuthor

    Agreed, and the framing I'd use is that neither case is a UXarray invention: xarray already treats sel on a dim with no coordinate as positional, so case A is just xarray semantics and case B is xarray's label lookup. So implement both by resolving labels to positions and forwarding to isel, keeping Grid.isel as the single place that rebuilds topology — uxgrid._ds.sel(n_face=7) would slice only the variables carrying n_face without renumbering face_node_connectivity or dropping orphaned nodes. The rule to agree on: sel is xarray's sel plus the grid comes along, with no other divergence.

    I think the cases here are referring to thoughts I sent via DM but didn't put in this issue yet, so I am posting them here for future reference:

    My thoughts are to avoid any complicated logical paths for now, and only implement the following for grid dims (n_face, n_edge, n_node):

    • (A) If the dim doesn't have separately-assigned coordinates, then sel() works just like isel(), because the implied coordinates are just the indices anyway.
    • (B) If the dim has separately-assigned coordinates, then sel() just uses xarray's sel() implementation, but for now also assert that the data is also on that dim. E.g., for now, if uxarr is face-centered, then uxarr.sel(n_face=7) just uses uxarr.data.sel(n_face=7) and uxarr.uxgrid._ds.sel(n_face=7). But, uxarr.sel(n_node=5) would fail with NotImplementedError.

    Now, responding to your message: great point about still needing to forward to Grid.isel() even in case B. Resolving labels to positions then forwarding to Grid.isel() will likely be a cleaner implementation than using Grid._ds.data.sel().

  8. moved this from 🏗 In progress to 👀 In review in UXarray Developmenton Aug 26, 2026
  9. moved this from 👀 In review to ✅ Done in UXarray Developmenton Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Fields

Priority

None yet

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions