1

是否可以删除 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)})
4

1 回答 1

5

您可以使用 xarray 的stackwhere方法来执行此操作。为了与您的示例保持一致,我还删除了 x/y 坐标,但这不是严格要求的。关键部分是:

  1. 使用堆栈展平x/y变暗dim_0
  2. 使用wherewithdrop=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
于 2018-05-17T21:33:32.747 回答