删除值并展平xarray Dataset的DataArrays

时间:2018-05-17 21:09:52

标签: python xarray

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

1 个答案:

答案 0 :(得分:3)

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

  1. 使用堆叠将x / y变暗展平为dim_0
  2. 使用wheredrop=True一起屏蔽并仅选择有限元素
  3. 这是一个例子,从你离开的地方开始......

    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