【发布时间】: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