pydata / xarray

N-D labeled arrays and datasets in Python
https://xarray.dev
Apache License 2.0
3.57k stars 1.07k forks source link

renaming axis coords and dims seem to keep some memory of older names #9542

Open jfpiolle opened 5 days ago

jfpiolle commented 5 days ago

What happened?

I think there is a bug when renaming axis dimensions and variables.

loading a file having the following coordinates:

import xarray as xr

ds = xr.open_dataset('model_test.nc')
print(ds.coords)

Coordinates:
  * longitude  (longitude) float32 -180.0 -179.5 -179.0 ... 178.5 179.0 179.5
  * latitude   (latitude) float32 -78.0 -77.5 -77.0 -76.5 ... 79.0 79.5 80.0
    time       datetime64[ns] ...

I want to rename these axes as lat and lon (for the sake of consistency between different datasets):

ds = ds.rename_vars({'longitude': 'lon', 'latitude': 'lat'}).swap_dims({'longitude': 'lon', 'latitude': 'lat'})
print(ds.coords)

which returns:

Coordinates:
  * lon      (lon) float32 -180.0 -179.5 -179.0 -178.5 ... 178.5 179.0 179.5
  * lat      (lat) float32 -78.0 -77.5 -77.0 -76.5 -76.0 ... 78.5 79.0 79.5 80.0
    time     datetime64[ns] ...

so far, everything is fine. Now I want to do a shallow copy of this dataset:

ds2 = ds.copy(deep=False)
print(ds2.coords)

which prints:

Coordinates:
  * lon      (longitude) float32 -180.0 -179.5 -179.0 ... 178.5 179.0 179.5
  * lat      (latitude) float32 -78.0 -77.5 -77.0 -76.5 ... 78.5 79.0 79.5 80.0
    time     datetime64[ns] ...

the lat / lon dimensions are now renamed as in the initial version (latitude/longitude)!

I attached below the input file I used.

test_model.nc.gz

What did you expect to happen?

I was expecting to have the dim and coord names as the dataset I copied.

Minimal Complete Verifiable Example

No response

MVCE confirmation

Relevant log output

No response

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS ------------------ commit: None python: 3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:45:18) [GCC 12.3.0] python-bits: 64 OS: Linux OS-release: 5.4.0-150-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: fr_FR.UTF-8 LOCALE: ('fr_FR', 'UTF-8') libhdf5: 1.14.3 libnetcdf: 4.9.2 xarray: 2024.9.0 pandas: 2.2.2 numpy: 2.1.0 scipy: 1.14.1 netCDF4: 1.7.1 pydap: None h5netcdf: 1.3.0 h5py: 3.11.0 zarr: None cftime: 1.6.4 nc_time_axis: None iris: None bottleneck: None dask: 2024.9.0 distributed: 2024.8.1 matplotlib: None cartopy: None seaborn: None numbagg: None fsspec: 2024.6.1 cupy: None pint: None sparse: None flox: None numpy_groupies: None setuptools: 72.2.0 pip: 24.2 conda: None pytest: 8.3.2 mypy: None IPython: 8.26.0 sphinx: 7.4.7
dcherian commented 5 days ago

Does a simple rename work for you instead of rename_vars + swap_dims? swap_dims is a bit broken at the moment.

max-sixty commented 5 days ago

Possibly a dupe of https://github.com/pydata/xarray/issues/8646