-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
transpose() does not appear to change dataset dimension order. #9921
Comments
Thanks for opening your first issue here at xarray! Be sure to follow the issue template! |
transposed = ds.transpose('time','loc','instrument')
assert ds["temperature"].dims != transposed["temperature"].dims |
Thanks @keewis . How would one then achieve this re-ordering of dimensions in |
you'd have to recreate the dataset from the variables (if I remember correctly... there might be a better way to do this): transposed = ds.transpose("time", "loc", "instrument")
recreated = xr.Dataset(
data_vars={k: arr.variable for k, v in transposed.data_vars.items()},
coords=transposed.coords,
attrs=transposed.attrs,
) However, you shouldn't need to do this, as the order in |
Thanks, appreciate it. I will try this. As you have pointed out, this is pretty inconsequential in xarray, but I have to often save netcdf files with strict dimension ordering and metadata so these other programs can read them without errors, e.g. R, NCL, and THREDDS data servers. Currently, I save the data with |
hmm... I wonder if we have an encoding setting for this? @dcherian, do you have any ideas? |
I don't think so, but I am also very confused. The dimension order in the file will follow the order of appearance as you iterate through variables AFAICT xarray/xarray/backends/common.py Lines 520 to 525 in 1622499
So what is |
Okay sure. Here's a real world example. It's a post-processed EPS file from ECMWF's open data. via to_netcdf:
Then I did a cdo copy of the same dataset:
Note the dimension ordering, which is I think this is what is required for other programs to read the netcdf correctly. I'm still debugging this though, hence why I've been trying to use "transpose". However, as @keewis suggested, I can just create a new dataset with the correct dimension ordering and populate it with the data. |
I think this is the real difference. CDO is doing some interpretation and adding attributes which Xarray will not do. You can specify Presumably some of the software you are using expects an "unlimited dim"?
Note that CF "recommends" it and the ordering is not required. Also note that the data variables have the dimension ordering you specified: EDIT: I missed this. It is still funny that |
FWIW I've tried adding the metadata attributes itself and it doesn't seem to make a difference, the only thing that works is the CDO copy, so that's why I'm trying the transposing of the dimensions now. Programs tested on:
Happy to share what I find :) |
What happened?
Hello,
I am using
.transpose()
on anxr.Dataset
in order to change the dimension order to make it cf-compliant, e.g. by having the dimension order be TIME, X, Y. While it appears to work when I do aprint
on the dataset, the dimension order doesn't actually change.What did you expect to happen?
I expect that the listed dimension order (ds.dims) updates, so that I can specify the exact order of the dimensions as required for CF-compliance.
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.12.8 | packaged by conda-forge | (main, Dec 5 2024, 14:24:40) [GCC 13.3.0]
python-bits: 64
OS: Linux
OS-release: 3.10.0-693.2.2.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_NZ.UTF-8
LOCALE: ('en_NZ', 'UTF-8')
libhdf5: 1.14.4
libnetcdf: 4.9.2
xarray: 2024.11.0
pandas: 2.2.3
numpy: 2.2.1
scipy: 1.14.1
netCDF4: 1.7.2
pydap: None
h5netcdf: 1.4.1
h5py: 3.12.1
zarr: None
cftime: 1.6.4
nc_time_axis: None
iris: 3.11.0
bottleneck: 1.4.2
dask: 2024.12.1
distributed: 2024.12.1
matplotlib: 3.10.0
cartopy: 0.24.0
seaborn: None
numbagg: None
fsspec: 2024.12.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 75.6.0
pip: 24.3.1
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None
The text was updated successfully, but these errors were encountered: