在应用程序上使用 Dask 返回多个列(一个 DataFrame 如此)

Using Dask on an apply returning several columns (a DataFrame so)

我正在尝试在应用程序上使用 dask,其函数输出 5 个浮点数。我将在此示例中进行简化。

def func1(row, param):
    return float(row.Val1) * param, float(row.Val1) * np.power(param, 2)

data = pd.DataFrame(np.array([["A01", 12], ["A02", 24], ["A03", 13]]), columns=["ID", "Val1"])

data2 = dd.from_pandas(data, npartitions=2).map_partitions(lambda df: df.apply(lambda row: func1(row, 2), axis=1, result_type="expand"), meta=pd.DataFrame()).compute(scheduler=get)

如果我不放置元数据,我会收到此错误消息:

ValueError: Metadata inference failed in `lambda`.

You have supplied a custom function and Dask is unable to 
determine the type of output that that function returns. 

To resolve this please provide a meta= keyword.
The docstring of the Dask function you ran should have more information.

Original error is below:
------------------------
ValueError("could not convert string to float: 'foo'", 'occurred at index 0')

如果我放一个元数据(虽然可能不合适...),我会得到这个:

ValueError: The columns in the computed data do not match the columns in the provided metadata

有人可以帮忙吗? :)

您提供的空 DataFrame 没有正确的列名。您没有在元数据中提供任何列,但您的输出确实包含它们。这是您的错误来源。

元值应与预期输出的列名和数据类型匹配。