【发布时间】:2021-03-04 12:10:01
【问题描述】:
我需要计算许多 NumPy 数组(最多可以是 4 维),一个用于 Dask 数据帧的每个分区,然后将它们添加为数组。但是,我正在努力让 map_partitions 为每个分区返回一个数组,而不是为所有分区返回一个数组。
import dask.dataframe as dd
import numpy as np, pandas as pd
df = pd.DataFrame(range(15), columns=['x'])
ddf = dd.from_pandas(df, npartitions=3)
def func(partition):
# Here I also tried returning the array in a list and in a tuple
return np.array([[1, 2], [3, 4]])
# Here I tried all the options available for 'meta'
results = ddf.map_partitions(func).compute()
那么results就是:
array([[1, 2],
[3, 4],
[1, 2],
[3, 4],
[1, 2],
[3, 4]])
如果我改为results.sum().compute(),我会得到30。
我想得到的是:
[np.array([[1, 2],[3, 4]]), np.array([[1, 2],[3, 4]]), np.array([[1, 2],[3, 4]])]
所以如果我计算总和,我会得到:
array([[ 3, 6],
[ 9, 12]])
您如何使用 Dask 实现此结果?
【问题讨论】: