首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xarray不能直接将xarray.Dataset转换为numpy数组

xarray不能直接将xarray.Dataset转换为numpy数组
EN

Stack Overflow用户
提问于 2021-06-30 10:19:23
回答 1查看 261关注 0票数 1

我遇到了一个错误,在尝试使用xarray时从数据集转换到数组时,我似乎无法解决这个错误。我遇到这个问题是因为我正在尝试向netcdf文件添加一个时间维度(打开netcdf,添加一个对所有数据都相同的时间戳,保存出netcdf)。

代码语言:javascript
复制
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

我收到的错误是

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-30 10:50:33

ds已经是一个xarray.DataArray。错误出现在下面这一行:

代码语言:javascript
复制
dst = ds.expand_dims(time=time_da) #errors here

因为虽然dsDataArray,但time_da不是。这应该是可行的:

代码语言:javascript
复制
dst = ds.expand_dims(time=time_da.to_array())
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68187652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档