From c00309560ad4adff8fcca3156506ef3098d32a9d Mon Sep 17 00:00:00 2001 From: Edward Evans Date: Tue, 14 Nov 2023 16:03:09 -0600 Subject: [PATCH] Use swap_dims and set_index to rename xarray dims 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. --- src/imagej/convert.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/imagej/convert.py b/src/imagej/convert.py index 8321e198..c84cc6ac 100644 --- a/src/imagej/convert.py +++ b/src/imagej/convert.py @@ -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):