Rename dims independently from coords?
See original GitHub issueI have a dataset that looks like this:
<xarray.Dataset>
Dimensions: (lat: 226, lon: 261, time: 7300)
Coordinates:
* lat (lat) float32 -32.0 -31.9 -31.8 -31.7 ... -9.700001 -9.6 -9.5
* lon (lon) float32 132.0 132.1 132.2 132.3 ... 157.7 157.8 157.9 158.0
* time (time) object 1980-01-01 15:00:00 ... 1999-12-31 15:00:00
Data variables:
rnd24 (time, lat, lon) float32 ...
Problem description
I would like to be able to rename the dataset dimensions, without renaming the coordinates.
Expected Output
<xarray.Dataset>
Dimensions: (y: 226, x: 261, time: 7300)
Coordinates:
* lat (y) float32 -32.0 -31.9 -31.8 -31.7 ... -9.700001 -9.6 -9.5
* lon (x) float32 132.0 132.1 132.2 132.3 ... 157.7 157.8 157.9 158.0
* time (time) object 1980-01-01 15:00:00 ... 1999-12-31 15:00:00
Data variables:
rnd24 (time, y, x) float32 ...
As far as I can tell, there is no way to do this. I can rename
the existing dims/coords to x/y, and then manually create new coordinates that are copies of x and y, which gets me to:
<xarray.Dataset>
Dimensions: (time: 7300, x: 261, y: 226)
Coordinates:
* y (y) float32 -32.0 -31.9 -31.8 -31.7 ... -9.700001 -9.6 -9.5
* x (x) float32 132.0 132.1 132.2 132.3 ... 157.7 157.8 157.9 158.0
* time (time) object 1980-01-01 15:00:00 ... 1999-12-31 15:00:00
lat (y) float32 -32.0 -31.9 -31.8 -31.7 ... -9.700001 -9.6 -9.5
lon (x) float32 132.0 132.1 132.2 132.3 ... 157.7 157.8 157.9 158.0
Data variables:
rnd24 (time, y, x) float32 ...
But it doesn’t seem to be possible to re-assign the new coordinates as the indexes for the existing dims.
In this case, it may seem a bit redundant, because the coordinates are equal to the grid. But I’m trying to get this output to work with code that also deals with other datasets that have non- rectilinear grids.
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (11 by maintainers)
Top Results From Across the Web
xarray - how to rename dimensions on a DataArray object
When you rename the dimensions, there's a new DataArray returned. And you have to assign that back to the old name.
Read more >ncrename(1): netCDF Renamer - Linux man page
ncrename renames dimensions, variables, and attributes in a netCDF file. ... a coordinate variable, you must separately rename both the dimension and the ......
Read more >Macro to rename dimensions in the SOLIDWORKS drawing view
VBA macro which allows to assign the custom name to the dimension in the SOLIDWORKS drawings view which otherwise cannot be renamed in...
Read more >xarray.Dataset.rename_dims
Returns a new object with renamed dimensions only. ... The desired names must not be the name of an existing dimension or Variable...
Read more >Converting NetCDF files to geo-referenced GeoTIFF
Rename the dimensions/coordinates of x/y so they are the same. xds = xds.rename_dims({"NX": "x", "NY": "y"}).rename_vars({"X": "x", ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @jukent, if you edit the first comment of your pull request you should be able to change it to Closes #3026 which will automatically close this issue when that PR is merged.
I will submit a pull request to add separate rename dims and rename coords functions in a day or two.