You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The DataArray.rename API is inconsistent with Dataset.rename and the documentation does not make that obvious.
Unlike Dataset.rename,
A DataArray cannot be renamed using a keyword argument, but fails saying "[name] is not a variable or dimension in this dataset" (example A below).
Passing the new name as a positional argument, but with other keyword arguments fails with the statement "the first argument to .rename must be a dictionary" (example B below).
As a consequence, renaming the array as well as its dimension(s)/coordinate(s) requires two separate function calls, i.e. da.rename("newname").rename("dim_0"="x").
Example A
da.rename does not recognize the array's current name as a keyword and returns a confusing error message.
>>>da.rename(alpha="beta")
---------------------------------------------------------------------------ValueErrorTraceback (mostrecentcalllast)
<ipython-input-98-af366146dc6f>in<module>---->1da.rename(alpha="beta", x="i")
xarray/core/dataarray.pyinrename(self, new_name_or_name_dict, **names)
1842 )
1843name_dict=either_dict_or_kwargs(new_name_or_name_dict, names, "rename")
->1844dataset=self._to_temp_dataset().rename(name_dict)
1845returnself._from_temp_dataset(dataset)
1846else:
xarray/core/dataset.pyinrename(self, name_dict, **names)
3390forkinname_dict.keys():
3391ifknotinselfandknotinself.dims:
->3392raiseValueError(
3393f"cannot rename {k!r} because it is not a "3394"variable or dimension in this dataset"ValueError: cannotrename'alpha'becauseitisnotavariableordimensioninthisdataset
Example B
da.rename will not change the name of the array as well as the dimension(s)/coordinate(s) names and emits an untrue error message, since the array cannot be renamed using a dict.
>>>da.rename("beta", x="i")
---------------------------------------------------------------------------ValueErrorTraceback (mostrecentcalllast)
<ipython-input-99-f90b078d6bd5>in<module>---->1da.rename("beta", x="i")
xarray/core/dataarray.pyinrename(self, new_name_or_name_dict, **names)
1841Mapping[Hashable, Hashable], new_name_or_name_dict1842 )
->1843name_dict=either_dict_or_kwargs(new_name_or_name_dict, names, "rename")
1844dataset=self._to_temp_dataset().rename(name_dict)
1845returnself._from_temp_dataset(dataset)
xarray/core/utils.pyineither_dict_or_kwargs(pos_kwargs, kw_kwargs, func_name)
284285ifnotis_dict_like(pos_kwargs):
-->286raiseValueError(f"the first argument to .{func_name} must be a dictionary")
287ifkw_kwargs:
288raiseValueError(
ValueError: thefirstargumentto .renamemustbeadictionary
What did you expect to happen?
No response
Minimal Complete Verifiable Example
No response
Relevant log output
No response
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.9.10 | packaged by conda-forge | (main, Feb 1 2022, 21:25:34)
[Clang 11.1.0 ]
python-bits: 64
OS: Darwin
OS-release: 21.1.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.1
libnetcdf: 4.8.1
What happened?
The DataArray.rename API is inconsistent with Dataset.rename and the documentation does not make that obvious.
Unlike Dataset.rename,
da.rename("newname").rename("dim_0"="x")
.Examples
Start with an array, da, and a dataset, ds
Dataset example
ds.rename renames everything at once using keyword arguments
Example A
da.rename does not recognize the array's current name as a keyword and returns a confusing error message.
Example B
da.rename will not change the name of the array as well as the dimension(s)/coordinate(s) names and emits an untrue error message, since the array cannot be renamed using a dict.
What did you expect to happen?
No response
Minimal Complete Verifiable Example
No response
Relevant log output
No response
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.9.10 | packaged by conda-forge | (main, Feb 1 2022, 21:25:34)
[Clang 11.1.0 ]
python-bits: 64
OS: Darwin
OS-release: 21.1.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.1
libnetcdf: 4.8.1
xarray: 0.21.1
pandas: 1.3.5
numpy: 1.22.2
scipy: 1.7.1
netCDF4: 1.5.7
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.5.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.2
dask: None
distributed: None
matplotlib: 3.5.0
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
setuptools: 60.7.1
pip: 22.0.3
conda: None
pytest: None
IPython: 7.29.0
sphinx: None
The text was updated successfully, but these errors were encountered: