Dask @delayed 将数据帧转换为 pandas

发布于 2025-01-11 15:39:19 字数 773 浏览 4 评论 0 原文

我有这段代码调用 dask @delayed 函数,该函数将 N 个 dask 数据帧作为输入并返回一个 dask 数据帧作为输出。

有两个问题(1)函数内部数据帧的类型是 pandas 而不是 dask,以及(2)当我得到函数的结果时,它也是 pandas 而不是 dask。

@delayed 获取 pandas 数据帧而不是 dask 作为输入背后的逻辑是什么?我只需要使用 dask 数据框。

这是代码:

df = pd.DataFrame({
    'height':  [6.21, 5.12, 5.85, 5.78, 5.98],
    'weight': [150, 126, 133, 164, 203]
})

df_dask = dd.from_pandas(df, npartitions=2)


@delayed
def some_function(*b):
    print('type b[0]: ' + str(type(b[0])) )
    ddf = b[0]
    return ddf

ddfout = some_function(df_dask, df_dask, df_dask)

computed = ddfout.compute()
>>> type b[0]: <class 'pandas.core.frame.DataFrame'> # this should be dask dataframe

type(computed)
>>> pandas.core.frame.DataFrame

I have this code that calls a dask @delayed function that takes N dask dataframes as input and returns a dask dataframe as output.

There are two problems (1) inside the function the type of the dataframe is pandas instead of dask, and (2) when I get the result of the function, it's also pandas instead of dask.

What is the logic behind @delayed to get as input pandas dataframes instead of dask? I need to work only with dask dataframes.

This is the code:

df = pd.DataFrame({
    'height':  [6.21, 5.12, 5.85, 5.78, 5.98],
    'weight': [150, 126, 133, 164, 203]
})

df_dask = dd.from_pandas(df, npartitions=2)


@delayed
def some_function(*b):
    print('type b[0]: ' + str(type(b[0])) )
    ddf = b[0]
    return ddf

ddfout = some_function(df_dask, df_dask, df_dask)

computed = ddfout.compute()
>>> type b[0]: <class 'pandas.core.frame.DataFrame'> # this should be dask dataframe

type(computed)
>>> pandas.core.frame.DataFrame

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

め七分饶幸 2025-01-18 15:39:19

获取 dask dataframe ddf0 的方法是:

ddf[0]

这里不需要延迟,API 已经提供了对所有支持的 pandas 方法(大多数其中)。

延迟适用于对常量和其他延迟值的任意操作,而不是像数据帧这样的 dask 集合。

请参阅文档: https://docs.dask.org/en/stable/delayed-best-practices.html#don-t-call-dask-delayed-on-other-dask-collections ;您可能想要ddf.map_partitions

The way to get column 0 of dask dataframe ddf is:

ddf[0]

There is no need for delayed here, the API already provides lazy operations over all the pandas methods that are supported (most of them).

Delayed is for arbitrary operations on constants and other delayed values, not dask collections like the dataframe.

See documentation: https://docs.dask.org/en/stable/delayed-best-practices.html#don-t-call-dask-delayed-on-other-dask-collections ; you probably wanted ddf.map_partitions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文