Open templiert opened 1 year ago
The coordinates are just a mapping from names to DataArrays, I don't think the order has any meaning (like it does for normal dicts).
Thanks.
On one hand I see your point: a mapping does not ensure order.
On the other hand, is it not counter-intuitive that the order of the dictionary-like container coords is not changed when using transpose_coords=True
?
Here my reasoning:
transpose_coords==True
And what should happen with non-dimension coordinates? Should they simply end up in the end?
The logic for this is not really trivial unless I am missing some obvious trick. You basically have to change the loop here: https://github.com/pydata/xarray/blob/ff6793d975ef4d1d8d5d32b8ad6f4f44e02dda9b/xarray/core/dataarray.py#L2919 To loop first over the new dims and potential coordinates with the same name and then over the rest of the coordinates (If possible in a single loop without code repetition).
Feel free to propose a PR!
Just noticed that the same logic does not work for Datasets, since all variables are kept in a common dict and the information about which are coordinates and which are data-variables is kept in a set, which is not ordered...
transpose_coords
is used to transpose coordinates with multiple dimensions:
In [1]: import xarray as xr
...:
...: ds = xr.tutorial.open_dataset("rasm")
...: ds.Tair.attrs.clear()
...: ds.Tair
Out[1]:
<xarray.DataArray 'Tair' (time: 36, y: 205, x: 275)>
[2029500 values with dtype=float64]
Coordinates:
* time (time) object 1980-09-16 12:00:00 ... 1983-08-17 00:00:00
xc (y, x) float64 ...
yc (y, x) float64 ...
Dimensions without coordinates: y, x
In [2]: ds.Tair.transpose("x", "y", ..., transpose_coords=False)
Out[2]:
<xarray.DataArray 'Tair' (x: 275, y: 205, time: 36)>
[2029500 values with dtype=float64]
Coordinates:
* time (time) object 1980-09-16 12:00:00 ... 1983-08-17 00:00:00
xc (y, x) float64 ...
yc (y, x) float64 ...
Dimensions without coordinates: x, y
In [3]: ds.Tair.transpose("x", "y", ..., transpose_coords=True)
Out[3]:
<xarray.DataArray 'Tair' (x: 275, y: 205, time: 36)>
[2029500 values with dtype=float64]
Coordinates:
* time (time) object 1980-09-16 12:00:00 ... 1983-08-17 00:00:00
xc (x, y) float64 ...
yc (x, y) float64 ...
Dimensions without coordinates: x, y
Interestingly, transpose_coords
is only an option for DataArray.transpose
, and it defaults to True
. This means that the example from https://github.com/pydata/xarray/issues/7294#issue-1452123685 always does the same thing, so even if we did implement the reordering nothing would change.
As such, I'm -0.5 on changing the order in which the coordinates are stored, since the only time that order is used is the repr
/ HTML repr
. In the past we have actually considered sorting the coordinates alphabetically, which did not happen because the coordinate names can be hashables of arbitrary types, and comparing a pair of hashables of different types is not easy.
What happened?
I used DataArray.transpose with
transpose_coords=True
to change the coords order fromstartings_dims = "dim_0", "dim_1", "dim_2"
toreordered_dims = "dim_2", "dim_1", "dim_0"
.The order of dims was correctly transposed but the order of coords remained unchanged.
What did you expect to happen?
I expected the transposed coords to be in the new order:
reordered_dims = "dim_2", "dim_1", "dim_0"
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment