2016-09-21 51 views
4

我需要將尺寸添加到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)]) 

我想我可以創建新的層面空DataArray中複製現有數據。

y = xr.DataArray(
    np.empty((b_size, a_size), 
    coords=([('b', b_coords), ('a', a_coords)]) 
y[:] = x 

一個更好的想法可能是使用concat。我花了一段時間才弄清楚如何爲concat維度指定dims和coords,而這些選項都不是很好。有什麼我錯過,可以使這個版本更清潔?

# 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'])) 

不過,是否有更好的方法來做到這一點比上述兩個選項之一?

回答

1

您已經對目前的選項進行了非常透徹的分析,實際上這些都不是很乾淨。

這對於xarray來說肯定是有用的函數,但沒有人能夠實現它。也許你會對幫忙感興趣?

一些API提案認爲這個問題GitHub的:https://github.com/pydata/xarray/issues/170

2

由於有這種數學被應用在新的層面我想,以增加新的維度繁殖的方式。

identityb = xr.DataArray(np.ones_like(b_coords), coords=[('b', b_coords)]) 
y = x * identityb 
相關問題