【问题标题】:Iteratively fill NaN values in xarray with dask用 dask 迭代地填充 xarray 中的 NaN 值
【发布时间】:2019-08-01 15:02:28
【问题描述】:

我有一个大型 3 维数据集(y、x、时间),其中存在显着差距 (NaN)。我想用上一次的值迭代地填充缺失值。

这是一个玩具示例:

import xarray as xr
import numpy as np

# 1. Generate a sample DataArray with missing values
dims = ('y', 'x', 't')
shape = (1000, 1000, 10)
coords = {d: np.arange(s) for d, s in zip(dims, shape)}
mask = np.random.randint(0, 2, shape)
data = np.where(mask, np.random.rand(*shape), np.nan)
da = xr.DataArray(data, dims=dims, coords=coords)

# 2. Write and reload from disk as dask array
da.to_netcdf('_tmp.nc')
da = xr.open_dataarray('_tmp.nc', chunks={'y': 100, 'x': 100, 't': 1})

# 3. Iteratively fill gaps
for t in range(1, len(da['t'])):
    # The following doesn't work with dask arrays
    da[{'t': t}] = da[{'t': t}].fillna(da[{'t': t-1}])

这可以正常工作,除了 dask 数组不支持项目分配,因此最后一行不起作用。我的数据集太大而无法读入内存,因此不能调用.load()

有没有什么方法可以以这种方式使用.fillna(),同时仍然使用通过 dask 提供的块的惰性求值?

我的真实数据约为10000x10000x100,包含多个变量。

【问题讨论】:

    标签: python dask python-xarray


    【解决方案1】:

    目前,Xarray 仅部分支持此类操作。理想情况下,您可以使用da.ffill(),但实现中存在一些现有问题,可能无法为您提供所需的结果(明确地说,xarray 目前不支持块之间的填充)。

    您可能需要查看此 GitHub 问题以尝试潜在的解决方法:https://github.com/pydata/xarray/issues/2699

    如果这看起来像你想要的,我鼓励你参与这个问题。

    【讨论】:

    • 现在看来da.ffill() 确实满足了我的需求,我可能可以相应地调整我的块大小。如果我遇到问题,我会很高兴回到 GitHub 问题。
    猜你喜欢
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    相关资源
    最近更新 更多