猿问

堆栈 xarray DataArray

我有 N 个 1Dxr.DataArray的 1 个array坐标b和 1 个scalar坐标a。我想将它们组合成一个DataArray带有array坐标b,的 2D a。这该怎么做?我试过了:


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

结果是 :


<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

现在我喜欢选择一个特别的“a”:


xcombined.sel(a=10)

然而,这引发了:


ValueError: dimensions or multi-index levels ['a'] do not exist


阿波罗的战车
浏览 188回答 1
1回答

白衣非少年

如果你提供dim给concat,这有效:xcombined = xr.concat([x1, x2], dim='a')进而:xcombined.sel(a=10)<xarray.DataArray (b: 3)>array([0, 1, 2])Coordinates:&nbsp; * b&nbsp; &nbsp; &nbsp; &nbsp; (b) int64 3 4 5&nbsp; &nbsp; a&nbsp; &nbsp; &nbsp; &nbsp; int64 10
随时随地看视频慕课网APP

相关分类

Python
我要回答