DataArray
にディメンションを追加して、新しいディメンションに値を入力する必要があります。ここに元の配列があります。私は新たな次元に空のDataArrayを作成して、既存のデータをコピーすることができると思いxarrayにディメンションを追加するDataArray
a_size = 10
a_coords = np.linspace(0, 1, a_size)
b_size = 5
b_coords = np.linspace(0, 1, b_size)
# original 1-dimensional array
x = xr.DataArray(
np.random.random(a_size),
coords=[('a', a coords)])
。
y = xr.DataArray(
np.empty((b_size, a_size),
coords=([('b', b_coords), ('a', a_coords)])
y[:] = x
良いアイデアはconcat
を使用することであるかもしれません。コンカットのディメンションとディメンションの両方を指定する方法を理解するのにはしばらく時間がかかりましたが、これらのオプションはどれも素晴らしいとは思われませんでした。このバージョンをきれいにすることができないものがありますか?
# specify the dimension name, then set the coordinates
y = xr.concat([x for _ in b_coords], 'b')
y['b'] = b_coords
# specify the coordinates, then rename the dimension
y = xr.concat([x for _ in b_coords], b_coords)
y.rename({'concat_dim': 'b'})
# use a DataArray as the concat dimension
y = xr.concat(
[x for _ in b_coords],
xr.DataArray(b_coords, name='b', dims=['b']))
上記の2つのオプションのどちらよりも優れた方法はありますか?