python-xarray rename data variable

Ray*_*ell 2 python-xarray

I've created a xarray.DataArray and I save it using xarray.DataArray.to_netcdf.

I create it using:

datatmp = np.full([nens, len(modanom.coords['time'].values), len(modanom.coords['latitude'].values), len(modanom.coords['longitude'].values)], np.nan)
b = xr.DataArray(datatmp, coords=[range(1,nens + 1), modanom.coords['time'], modanom.coords['latitude'], modanom.coords['longitude']], dims=['ensemble', 'time', 'latitude', 'longitude'])
Run Code Online (Sandbox Code Playgroud)

i.e. I don't specify a name such as:

b = xr.DataArray({'windspeed': (('ensemble', 'time', 'latitude', 'longitude'), datatmp)}, coords=[range(1,nens + 1), modanom.coords['time'], modanom.coords['latitude'], modanom.coords['longitude']], dims=['ensemble', 'time', 'latitude', 'longitude'])
Run Code Online (Sandbox Code Playgroud)

I do some manipulation of this file and obtain root mean square error and the resultant xarray.Dataset is

<xarray.DataArray (latitude: 81, longitude: 131, time: 1)>
array([[[      nan],
    [      nan],
    ..., 
    [      nan],
    [      nan]],

   [[      nan],
    [      nan],
    ..., 
    [      nan],
    [      nan]],

   ..., 
   [[      nan],
    [ 0.843295],
    ..., 
    [ 0.794338],
    [      nan]],

   [[      nan],
    [      nan],
    ..., 
    [      nan],
    [      nan]]])
Coordinates:
* latitude   (latitude) float64 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ...
* longitude  (longitude) float64 260.0 261.0 262.0 263.0 264.0 265.0 266.0 ..
* time       (time) datetime64[ns] 1983-01-15T12:00:00
Run Code Online (Sandbox Code Playgroud)

When reading in the netCDF file I have a xarray.Dataset

which looks like:

<xarray.Dataset>
Dimensions:                        (latitude: 81, longitude: 131, time: 1)
Coordinates:
* latitude                       (latitude) float64 0.0 1.0 2.0 3.0 4.0 ...
* longitude                      (longitude) float64 260.0 261.0 262.0 ...
* time                           (time) datetime64[ns] 1983-01-15T12:00:00
Data variables:
__xarray_dataarray_variable__  (latitude, longitude, time) float64 nan ...
Attributes:
_NCProperties:  version=1|netcdflibversion=4.4.1|hdf5libversion=1.8.18
Run Code Online (Sandbox Code Playgroud)

Can I rename the __xarray_dataarray_variable__?

The reason I ask is because as I have functions which create xarray.Dataset's and therefore I don't specify a data variable name. However, I would like to rename it before plotting.

Ray*_*ell 6

DataArray.rename
Run Code Online (Sandbox Code Playgroud)

就是我一直在寻找的


chu*_*axr 6

数据集 ds 的另一种可能性是:

    ds['new_var_name'] = ds['__xarray_dataarray_variable__']
    ds = ds.drop(['__xarray_dataarray_variable__'])
Run Code Online (Sandbox Code Playgroud)