[英]add new coordinates to an xarray.DataArray
Assume the following code, which has an xarray.DataArray
with two dimensions and a coordinate: 假设以下代码具有一个xarray.DataArray
,其中包含两个维度和一个坐标:
import numpy as np
from xarray import DataArray
data = np.random.rand(10, 4)
f_names = ['a', 'b', 'c', 'd']
sample_weights = np.random.rand(10)
rows = list(range(len(data)))
coords={'samples': rows,
'features': f_names,
'sample_weights': ('samples', sample_weights)}
xdata = DataArray(data, coords=coords,
dims=['samples', 'features'])
subset = xdata[::2]
Now I want to add another coordinates, like alternate_sample_weights
to subset
. 现在,我想向subset
添加另一个坐标,例如alternate_sample_weights
。 I try: 我尝试:
subset.assign_coords(alternate_sample_weights=np.zeros(5)
which results in the following error: 导致以下错误:
ValueError: cannot add coordinates with new dimensions to a DataArray
The API documentation is pretty sparse, not sure what I'm doing wrong. API文档非常稀疏,不确定我在做什么错。
It appears that when adding a new coordinate, you need to pass also the dimension along which it's added. 似乎在添加新坐标时,还需要传递添加坐标的尺寸。 So in this case, it would be: 因此,在这种情况下,它将是:
subset.assign_coords(
alternate_sample_weights=('samples', np.zeros(5)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.