我遇到了一个错误,在尝试使用xarray时从数据集转换到数组时,我似乎无法解决这个错误。我遇到这个问题是因为我正在尝试向netcdf文件添加一个时间维度(打开netcdf,添加一个对所有数据都相同的时间戳,保存出netcdf)。
import xarray as xr
import pandas as pd
scriptpath = os.path.dirname(os.path.abspath(__file__))
outputfile = scriptpath + '\\20210629_deadgrass.aus.nc'
times= pd.to_datetime(str(yesterday.strftime('%Y%m%d')))
time_da = xr.Dataset({"time": times})
arr = xr.open_dataset(outputfile)
ds = arr.to_array()
dst = ds.expand_dims(time=time_da) #errors here我收到的错误是
Exception has occurred: TypeError
cannot directly convert an xarray.Dataset into a numpy array. Instead, create an xarray.DataArray first, either with indexing on the Dataset or by invoking the `to_array()` method.
File "Z:\UpdateAussieGRASS.py", line 101, in <module>
dst = ds.expand_dims(time=time_da)我似乎找不出倒数第二行的to_array()做错了什么。here就是to_array()的例子。自动生成的文档是here。
发布于 2021-06-30 10:50:33
ds已经是一个xarray.DataArray。错误出现在下面这一行:
dst = ds.expand_dims(time=time_da) #errors here因为虽然ds是DataArray,但time_da不是。这应该是可行的:
dst = ds.expand_dims(time=time_da.to_array())https://stackoverflow.com/questions/68187652
复制相似问题