尝试将 xarray 保存到 netcdf 时出现 TypeError

TypeError when trying to save xarray to netcdf

如有任何帮助,我将不胜感激。我有一个 xarray,它是使用 .to_xarray() 方法从 pandas 数据帧创建的,具有以下形式:

<xarray.Dataset>
Dimensions:  (lat: 556, lon: 1438, time: 96)
Coordinates:
  * time     (time) datetime64[ns] 2005-01-01 2006-01-01 ... 2100-01-01
  * lat      (lat) float64 -55.38 -55.12 -54.88 -54.62 ... 82.88 83.12 83.38
  * lon      (lon) float64 -69.88 -69.62 -69.38 -69.12 ... -26.12 -24.38 -24.12
Data variables:
    1.0      (lat, lon, time) float32 nan nan nan nan nan ... nan nan nan nan
    2.0      (lat, lon, time) float32 nan nan nan nan nan ... nan nan nan nan
    3.0      (lat, lon, time) float32 nan nan nan nan nan ... nan nan nan nan
    4.0      (lat, lon, time) float32 nan nan nan nan nan ... nan nan nan nan
    5.0      (lat, lon, time) float32 nan nan nan nan nan ... nan nan nan nan
    6.0      (lat, lon, time) float32 nan nan nan nan nan ... nan nan nan nan
    7.0      (lat, lon, time) float32 nan nan nan nan nan ... nan nan nan nan

当我尝试使用 .to_netcdf() 保存它时,出现以下错误:

TypeError: DataArray.name or Dataset key must be either a string or None for serialization to netCDF files

我尝试通过 ds_land[1.0].name = 'class_1' 等更改变量的名称,但这也会产生相同的错误。有什么想法吗?

您需要使用 rename() to rename your variable to a string before serializing, rather than setting the name attribute on your DataArray. This is because variables names in netCDF must be strings.

import xarray as xr
ds = xr.Dataset()
ds[1.0] = xr.DataArray([1, 2, 3])

ds[1.0].attrs['name'] = 'class_1'
ds.to_netcdf('testing.nc') # this fails, as you have seen

ds = ds.rename({1.0: 'class_1'})
ds.to_netcdf('testing2.nc') # this works

因此将您的变量重命名为 '1.0' 也可以解决问题:

ds = xr.Dataset()
ds[1.0] = xr.DataArray([1, 2, 3])
ds = ds.rename({1.0: '1.0'})
ds.to_netcdf('testing3.nc') # this also works

顺便说一句,考虑到你如何命名你的变量,看起来它们作为另一个维度可能会更好,所以你有一个维度为 latlon 的新变量, time, <new dim with levels 1..7>.