是否可以删除xarray数据集中包含的DataArrays值,以便xr_dataset
在下面的示例代码中转换为xr_flat
?
import xarray as xr
import numpy as np
n = np.nan
a = np.array([[n,n,1],
[1,2,2],
[2,n,n]], dtype='float32')
b = np.random.rand(3,3)
xr_dataset = xr.Dataset({'a': xr.DataArray(a, dims=['x', 'y']),
'b': xr.DataArray(b, dims=['x', 'y'])})
a_flat = a[np.isfinite(a)]
b_flat = b[np.isfinite(a)]
xr_flat = xr.Dataset({'a': xr.DataArray(a_flat),
'b': xr.DataArray(b_flat)})
答案 0 :(得分:3)
您可以使用xarray的stack
和where
方法执行此操作。为了与你的例子保持一致,我也删除了x / y坐标,但这并不是严格要求的。关键部分是:
x
/ y
变暗展平为dim_0
where
与drop=True
一起屏蔽并仅选择有限元素这是一个例子,从你离开的地方开始......
In [2]: ds_stack = xr_dataset.stack(dim_0=('x', 'y'))
In [3]: ds_stack = ds_stack.reset_index('dim_0').drop(['x', 'y'])
In [4]: ds_stack.where(np.isfinite(ds_stack['a']), drop=True)
Out[4]:
<xarray.Dataset>
Dimensions: (dim_0: 5)
Dimensions without coordinates: dim_0
Data variables:
a (dim_0) float32 1.0 1.0 2.0 2.0 2.0
b (dim_0) float64 0.8642 0.05446 0.3728 0.7797 0.9501
In [5]: xr_flat
Out[5]:
<xarray.Dataset>
Dimensions: (dim_0: 5)
Dimensions without coordinates: dim_0
Data variables:
a (dim_0) float32 1.0 1.0 2.0 2.0 2.0
b (dim_0) float64 0.8642 0.05446 0.3728 0.7797 0.9501