我有一个 3d 数组 (10x10x3),由于某种原因,它被保存为 2d xr.DataArray (100x3)。它看起来有点像这样:
data = xr.DataArray(np.random.randn(100, 3),
dims=('ct', 'x'),
coords={'ct': range(100)})
c = [x%10 for x in range(100)]
t = [1234+x//10 for x in range(100)]
Run Code Online (Sandbox Code Playgroud)
c 和 t 是在 ct 中捆绑在一起的坐标。
过去我已经解决了分离二维的问题,如下:
t_x_c,x = data.shape
nc = 10
data = np.reshape(data.values,(t_x_c//nc,nc, x))
Run Code Online (Sandbox Code Playgroud)
但这需要数据结构中的许多假设,这些假设在不久的将来可能不成立(例如,c 和 t 可能不像我的示例中那样规则)。
我已设法将 c 和 t 作为附加坐标分配给数组:
data2 = data.assign_coords(
coords={"c": ("ct", c),
"t": ("ct", t),
},)
Run Code Online (Sandbox Code Playgroud)
但我想将它们提升到数组的维度。我该怎么做呢?