Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用dask迭代地填充xarray中的NaN值_Python_Dask_Python Xarray - Fatal编程技术网

Python 用dask迭代地填充xarray中的NaN值

Python 用dask迭代地填充xarray中的NaN值,python,dask,python-xarray,Python,Dask,Python Xarray,我有一个大的三维数据集(y,x,time),其中有很大的间隙(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(di

我有一个大的三维数据集(y,x,time),其中有很大的间隙(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提供的块的惰性计算


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

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

您可能希望查看此GitHub问题,以尝试一种潜在的解决方法:


如果这似乎是您的目的,我鼓励您参与这个问题。

目前看来,
da.ffill()。如果我遇到问题,我会很高兴地回到GitHub问题上。