[英]Stack xarray DataArray
I have N 1D xr.DataArray
's with an 1 array
coordinate b
and 1 scalar
coordinate a
. 我有N个1D xr.DataArray
,其中1个array
坐标b
和1个scalar
坐标a
。 I want to combine them to a 2D DataArray
with array
coordinates b
, a
. 我想将它们合并到2D DataArray
有array
坐标b
, a
。 How to do this? 这个怎么做? I have tried: 我努力了:
x1 = xr.DataArray(np.arange(0,3)[...,np.newaxis], coords=[('b', np.arange(3,6)),('a', [10])]).squeeze()
x2 = xr.DataArray(np.arange(0,3)[...,np.newaxis], coords=[('b', np.arange(3,6)),('a', [11])]).squeeze()
xcombined = xr.concat([x1, x2])
xcombined
Results in : 结果是 :
<xarray.DataArray (concat_dims: 2, b: 3)>
array([[0, 1, 2],
[0, 1, 2]])
Coordinates:
* b (b) int64 3 4 5
a (concat_dims) int64 10 11
Dimensions without coordinates: concat_dims
Now I like to select a particularly 'a': 现在,我想选择一个特别的“ a”:
xcombined.sel(a=10)
However, this raises: 但是,这引起了:
ValueError: dimensions or multi-index levels ['a'] do not exist
If you supply dim
to concat
, this works: 如果您为concat
提供dim
,则可以使用:
xcombined = xr.concat([x1, x2], dim='a')
And then: 接着:
xcombined.sel(a=10)
<xarray.DataArray (b: 3)>
array([0, 1, 2])
Coordinates:
* b (b) int64 3 4 5
a int64 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.