I've noticed that Dataset.transpose transposes the coordinates while DataArray.transpose does not.
Example:
import xarray as xr
import numpy as np
X_da = xr.DataArray(
np.random.random((100, 10)),
coords={'coord_1': (['sample', 'feature'], np.ones((100, 10)))},
dims=('sample', 'feature')
)
In []: X_da.transpose().coord_1.dims
Out[]: ('sample', 'feature')
X_ds = xr.Dataset(
{'var_1': (['sample', 'feature'], np.random.random((100, 10)))},
coords={'coord_1': (['sample', 'feature'], np.ones((100, 10)))},
)
In []: X_ds.transpose().coord_1.dims
Out[]: ('feature', 'sample')
This behaviour is probably intentional, but there are cases where I'd like DataArray.transpose to also transpose the coordinates. As a workaround, I have to convert to a Dataset, transpose and convert back.
I was thinking that DataArray.transpose could accept a keyword argument transpose_coords which would be False by default. I could work on a PR implementing this behavior if it's desired.
To be honest, I'm not sure skipping coordinates was actually intentional. In most cases, I suspect transposing coordinates simply doesn't matter because either (1) there are no multi-dimensional coordinates or (2) xarray usually doesn't care much about dimension order (so it doesn't matter).
I would support adding a transpose_coords argument, but we should also consider switching the default behavior in the future to transpose_coords=True.
Anyone else have opinions on this API?
Also hitting this issue. (Use case: formatting netcdf files for some R code that does not have labeled indexing... ugh). Thanks @phausamann for the work around. Default transposing of coods makes sense to me.
Honestly, we could probably consider the DataArray behavior to be a bug. In general DataArray methods should work exactly like Dataset methods on a Dataset with a single data variable. (In fact, this is how we implement many DataArray methods.)
Most helpful comment
To be honest, I'm not sure skipping coordinates was actually intentional. In most cases, I suspect transposing coordinates simply doesn't matter because either (1) there are no multi-dimensional coordinates or (2) xarray usually doesn't care much about dimension order (so it doesn't matter).
I would support adding a
transpose_coordsargument, but we should also consider switching the default behavior in the future totranspose_coords=True.Anyone else have opinions on this API?