Skip to content

.subset with da that has time dimension #1444

Closed

Description

@Chrismarsh

Version

2025.11.0

How did you install UXarray?

Source

What happened?

I have a dataset that looks like

>>> da = du.sel(time=["2018-04-28 00:00:00", "2023-03-22 00:00:00"])[['Elevation']]
>>> print(da)

<xarray.UxDataset> Size: 1GB
Dimensions:       (met: 2, time: 2, n_face: 28495325)
Coordinates:
  * time          (time) datetime64[ns] 16B 2018-04-28 2023-03-22
  * met           (met) object 16B 'hrdps' 'capa'
    Mesh2_face_x  (n_face) float64 228MB -123.8 -123.4 -123.9 ... -116.3 -116.3
    Mesh2_face_y  (n_face) float64 228MB 46.49 46.61 46.49 ... 52.56 52.33 52.34
Dimensions without coordinates: n_face
Data variables:
    Elevation     (met, time, n_face) float64 912MB dask.array<chunksize=(1, 1, 1000000), meta=np.ndarray

which I want to clip to a lat lon box:

tmp = da.Elevation.subset.bounding_box(lon, lat)

but it errors as:

IndexError: dimension coordinate 'time' conflicts between indexed and indexing objects:
<xarray.DataArray 'time' (time: 2)> Size: 16B
array(['2018-04-28T00:00:00.000000000', '2023-03-22T00:00:00.000000000'],
      dtype='datetime64[ns]')
Coordinates:
  * time     (time) datetime64[ns] 16B 2018-04-28 2023-03-22
Attributes:
    standard_name:  time
    long_name:      Time
vs.
<xarray.Variable ()> Size: 8B
array('2016-10-01T00:00:00.000000000', dtype='datetime64[ns]')
Attributes:
    standard_name:  time
    long_name:      Time

which I don't totally understand. Weirdly, this code used to work, I want to say 1 or 2 releases ago.

What did you expect to happen?

It to subset across all dim, time included

Activity

  1. added
    bug
    on Jan 23, 2026
  2. rajeeja commented on Jul 8, 2026

    @rajeeja
    Contributor
  3. Sevans711 commented on Jul 10, 2026

    @Sevans711
    Collaborator

    Hi @Chrismarsh, thank you for your bug report. Can you confirm if this is still an issue with the latest version of uxarray?

    I tried reproducing using one of the uxarray tutorial datasets which has a time dimension:

    full_ds = ux.tutorial.open_dataset("outCSne30-timeseries", chunks={'time': 1})
    ds = full_ds.sel(time=['2018-04-28 00:00:00', '2018-04-28 01:00:00'])[['psi']]
    print(ds)
    <xarray.UxDataset> Size: 43kB
    Dimensions:  (time: 2, n_face: 5400)
    Coordinates:
      * time     (time) datetime64[ns] 16B 2018-04-28 2018-04-28T01:00:00
    Dimensions without coordinates: n_face
    Data variables:
        psi      (time, n_face) float32 43kB dask.array<chunksize=(1, 5400), meta=np.ndarray>
    Attributes:
        source:   Synthetic field for sel regression
    
    lon = [-70, 70]
    lat = [-50, 50]
    tmp = ds.psi.subset.bounding_box(lon, lat)
    print(tmp)
    <xarray.UxDataArray 'psi' (time: 2, n_face: 1576)> Size: 13kB
    dask.array<getitem, shape=(2, 1576), dtype=float32, chunksize=(1, 1576), chunktype=numpy.ndarray>
    Coordinates:
      * time     (time) datetime64[ns] 16B 2018-04-28 2018-04-28T01:00:00
    Dimensions without coordinates: n_face
    

    In this case, on uxarray version 2026.6.0, it seems to work okay!

    If you are still seeing the bug on your end, could you help me reproduce it? If you can provide a small example where it still occurs, I should be able to look into it further and hopefully get things working properly again.

  4. moved this from 📚 Backlog to 🏗 In progress in UXarray Developmenton Jul 17, 2026
  5. erogluorhan commented on Jul 17, 2026

    @erogluorhan
    Member

    Hi @Chrismarsh , please let us know if this is fixed on your end as well.

  6. Chrismarsh commented on Jul 17, 2026

    @Chrismarsh
    Author

    Hi @erogluorhan . I'm just on parental leave at the moment and don't have the capacity to test this until this fall. I will let you know if it's still a problem. Feel free to close this if you wish.

  7. moved this from 🏗 In progress to 👀 In review in UXarray Developmenton Jul 17, 2026
  8. erogluorhan commented on Jul 17, 2026

    @erogluorhan
    Member

    Congrats @Chrismarsh ! No worries at all. We will run one more check on this and then close if things seem fixed.

  9. dylannelson commented on Jul 31, 2026

    @dylannelson
    Member

    After looking at this report for a bit and trying to reproduce it, I had a few takeaways

    To Reproduce: (versions | uxarray: <= 2026.7.1.dev6+g4278eb6b7 | <= xarray: 2026.7.0)

    1. Read in a grid file /uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc
    2. Give it time coordinates (if it doesn't have one already ds_t = ds.assign_coords(time=pd.Timestamp('2016-10-01'))
    3. Create elevation data
      •   times = pd.to_datetime(['2018-04-28', '2023-03-22'])
          n_face = uxgrid.n_face
          elevation = ux.UxDataArray(
              data=np.random.rand(len(times), n_face),
              dims=['time', 'n_face'],
              coords={'time': times},
              name='Elevation',
              uxgrid=uxgrid,
          ) 
    4. Try to get a subset out = elevation.subset.bounding_box((-180, 180), (-90, 90))

    Error:

    IndexError: dimension coordinate 'time' conflicts between indexed and indexing objects:
    <xarray.DataArray 'time' (time: 2)> Size: 16B
    array(['2018-04-28T00:00:00.000000', '2023-03-22T00:00:00.000000'],
          dtype='datetime64[us]')
    Coordinates:
      * time     (time) datetime64[us] 16B 2018-04-28 2023-03-22
    vs.
    <xarray.Variable ()> Size: 8B
    array('2016-10-01T00:00:00.000000000', dtype='datetime64[ns]')

    Takeway

    • From what I have seen and tested, on every version of uxarray/xarray I've tested on, there seems to be a problem when getting a subset if the grid has time coordinates.
    • The team suggests that time coordinates are very often not in grid files, so this is not something that is an expected behavior. Changes may be possible to help check for these odd cases.

    Fix

    This is built on the assumption that the grid you are using has a time coordinate, which I deduced from the error, but if it doesn't then I'm not sure where the 2016 date is coming from in the error. If it does have time, consider dropping it and seeing if running the command after works like this:

    du.uxgrid._ds = du.uxgrid._ds.drop_vars("time", errors="ignore")
    tmp = da.Elevation.subset.bounding_box(lon, lat) 

    This removes only the grid's scalar time (2016-10-01), which is a separate object from the data's time dimension. The grid dataset (uxgrid._ds) holds mesh topology, not the data. The data's time = [2018-04-28, 2023-03-22] lives on Elevation and is untouched, so the subset result still has time and sel(time=...) / sortby("time") keep working exactly as before.

    Xarray reproduction:

    If you want to reproduce this same error in xarray, you can try

    N_FACE = 20
    # --- The DATA: a face-centered variable with a real multi-valued `time` dim coordinate ---
    data = xr.DataArray(
        np.random.rand(2, N_FACE),
        dims=["time", "n_face"],
        coords={"time": pd.to_datetime(["2018-04-28", "2023-03-22"])},
        name="Elevation",
    )
    grid_ds = xr.Dataset({"subgrid_face_indices": ("n_face", np.arange(5, 12))},)
    grid_ds = grid_ds.assign_coords(time=pd.Timestamp("2016-10-01"))
    face_indexer = grid_ds["subgrid_face_indices"]
    out = data.isel(n_face=face_indexer)

    But if you replace the last line with:
    out = data.isel(n_face=face_indexer.values) (note the .values)
    it works again

    Follow up work:

    I was able to make some extensive notebooks and notes for this issue, in xarray, uxarray, and potential fixes. If anyone is exploring this in the future and wants the folder, let me know.

    For now the team has no backend solution for handling grids that may have incidents like this, but if we got more information, like the grid that was used here, or the code leading up to .subset being used, it could help a lot at further diagnosing the problem

  10. Sevans711 commented on Jul 31, 2026

    @Sevans711
    Collaborator

    Thank you for looking into this in detail @dylannelson! So, basically, if your grid's "time" dimension does not line up with your UxDataset or UxDataArray's "time" dimension, then that will cause a crash. Am I understanding that correctly?

    If yes, it does make sense that that would cause a crash, but my initial suspicion is that "uxgrid has time dimension" would never occur in uxarray directly, even if inputting a .nc file with "time" dimension as the grid file, due to how uxarray parses grid files. I haven't confirmed this yet though. If you have more time to look into this, would you be able to try constructing a grid file with a time dimension (e.g., load existing file using xarray, add data var with time dimension, then tell xarray to write result as a netcdf file), then loading it into uxarray as a grid (e.g. ux.open_grid(...)) and then confirm whether the resulting Grid object actually has the "time" dimension or not?

    Also, can you provide a complete example of your steps (1) and (2)? How did you read in the grid file (just ux.open_grid()?) and how did you give it time coordinates (what did you do with ds_t after creating it, did you do something like uxgrid._ds = ds_t)? If getting to this crash always requires messing with suggested-to-be-private attributes (i.e. attributes starting with underscore) then I don't think we need to worry about fixing it.

    Separately, there is a question of whether something like this is the actual cause of the original bug in this issue report, or a different bug entirely. At this point I still think more information would be required about the original crash, before we can be confident that it has been reproduced. If you are able to show that ux.open_grid(...) can lead to grids with time dimension (which would subsequently lead to the crash you found), I think it would be worthwhile to report that as a separate issue and work on fixing it (e.g. add checks during Grid creation to ensure there are no unexpected dimensions like "time").

  11. 1 remaining item

  12. Sevans711 commented on Jul 31, 2026

    @Sevans711
    Collaborator

    Ah! Ok a quick reaction: my grid and data are in the same file. I'll open the file twice: once to get the grid object and once to get the data. This always ends up with time on the grid.

    Oh! Thank you for your quick reply, that is extremely helpful. That is enough to convince me that @dylannelson has almost certainly found your bug - nice! Now we should hopefully be able to reproduce, fix, and test. I will let @dylannelson take the lead on that; I think many of my suggested next steps from the message above still apply. (Clarifying, this means I was mistaken earlier. In fact, "uxgrid has time dimension" apparently can occur if grid file had time dimension. But it should not occur, time should not be included in the Grid object even if it is included in the input file.)

    Flagging as related: #1461

    Sidenote, onefile open_dataset is now supported for cases like this, see #1479 for more details! This probably wouldn't fix the bug, but it does mean you could do ux.open_dataset(yourfile) instead of ux.open_dataset(yourfile, yourfile), which may be slightly more convenient and should give equivalent results.

  13. dylannelson commented on Jul 31, 2026

    @dylannelson
    Member

    @Sevans711
    time can also exist on a grid as seen by this notebook (and won't go away unless manually removed) in both xarray and uxarray:

    06-grid-time-roundtrip-test(1).html

    That notebook was the suggested order of testing to see if uxarray will allow the time coordinates through:

    1. Opened one of the demo grids in xarray
    2. added time to it (not a thing that we expect people to normally do, but shows that if it does happen, it will persist)
    3. Save it
    4. open with ux.open_grid()
    5. see if time coordinates can be called (they can)
    6. Reproduce the error with this data

    "uxgrid has time dimension" would never occur in uxarray directly, even if inputting a .nc file with "time" dimension as the grid file, due to how uxarray parses grid files

    I think @erogluorhan had the same suspicion but I am thinking that what we have been observing here may show that this is not the case

  14. moved this from 👀 In review to 🏗 In progress in UXarray Developmenton Jul 31, 2026
  15. Sevans711 commented on Jul 31, 2026

    @Sevans711
    Collaborator

    time can also exist on a grid as seen by this notebook (and won't go away unless manually removed) in both xarray and uxarray:

    Thank you for looking into this and sharing that notebook, it looks to me like you've done a great job demonstrating clearly here that yes, indeed, grid-reading fails to properly strip extra dimensions, like "time".

    I think the next step would probably be to work on the fix and some tests, and opening up a PR, if that sounds good! If you get stuck or have questions feel free to either post updates here or send me a direct message separately, I would be happy to help as needed.

  16. dylannelson commented on Jul 31, 2026

    @dylannelson
    Member

    @Sevans711 Yeah I think it may help for a few of us to have a call on this one as it develops into its own PR/issue. @erogluorhan and I already had a few thoughts/ideas about where/how it needs to be placed without any certain ways forward. I'll do more digging in the meantime. Depending on where it goes it may have a handful of downstream effects.

  17. erogluorhan commented on Jul 31, 2026

    @erogluorhan
    Member

    Thank you for further looking into this and finding this case, @dylannelson ! A few thoughts/additions:

    1. Having a time dim in grid definition files should be rare, but @Chrismarsh 's this example showed us it is possible
    2. Even in this case, Grid object itself has no direct property for time dimension, but we had an internal Grid._ds of xarray.Dataset type to store variables coming from the grid file, and it receives even non-coordinate variables such as time in this case.

    We'll need to look into where we're populating Grid._ds in our I/O, and if we can avoid such contamination.

  18. added
    extra dims
    on Aug 11, 2026
  19. moved this from 🏗 In progress to 👀 In review in UXarray Developmenton Aug 21, 2026
  20. moved this from 👀 In review to 🏗 In progress in UXarray Developmenton Nov 6, 2026
  21. moved this from 🏗 In progress to ✅ Done in UXarray Developmenton Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugextra dims

Type

No type

Fields

Priority

High

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions