Skip to content

Commit

Permalink
Use swap_dims and set_index to rename xarray dims
Browse files Browse the repository at this point in the history
The xarray team is currently deprecating how the
xarray.DataArray.rename() method works. Specifcally, renaming an
xarray.DataArray no longer creates an indexed coordinate. Using the
rename() method will generate the following warning for a given dimesion:

580: UserWarning: rename 't' to 't' does not create an index anymore. Try using swap_dims instead or use set_index after rename to create an indexed coordinate.
    return xarr.rename(dim_map)

This commit uses swap_dims() with set_index() to swap the
dimensions (i.e. rename them) and then reassert the indexed coordinates.
  • Loading branch information
elevans committed Nov 14, 2023
1 parent 5111b9a commit c003095
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/imagej/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,11 @@ def _rename_xarray_dims(xarr, new_dims: Sequence[str]):
for i in range(xarr.ndim):
dim_map[curr_dims[i]] = new_dims[i]

return xarr.rename(dim_map)
# swap dims and set indexed coordinates
coord_map = {v: k for k, v in dim_map.items()}
xarr = xarr.swap_dims(dim_map)

return xarr.set_index(coord_map)


def _delete_labeling_files(filepath):
Expand Down

0 comments on commit c003095

Please sign in to comment.