Dask @delayed 将数据帧转换为 pandas
我有这段代码调用 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
获取 dask dataframe
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 dataframeddf
is: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
.