【问题标题】:Applying a function to a dask dataframe results in a TypeError deriving from the args将函数应用于 dask 数据帧会导致从 args 派生的 TypeError
【发布时间】:2020-03-27 17:36:13
【问题描述】:

我有一段代码涉及一个函数和一个 dask 数据帧 (df1)。数据框在函数之前已经存在,所以当我执行meta=df1.dtypes 时,我正在调用现有类型。这是代码:

def my_function(group, arg1, arg2, arg3):
    # some operations

df1 = df1.groupby("Sequence").apply(my_function, args=[arg1, arg2, arg3], meta=df1.dtypes)

它返回以下错误:

TypeError: my_function() got an unexpected keyword argument 'args'

有人知道为什么吗?我无法在线找到解决方案,并且我正在遵循 DASK API 中的指南,所以我不明白为什么会出现错误。 args=... 应该被正确解析,它们代表函数的 附加 参数(第一个是 groupby() 的结果。

【问题讨论】:

    标签: python pandas dataframe pandas-groupby dask


    【解决方案1】:

    就像在 pandas 中一样,您在 *args 中提供额外的参数,而不是名为 args 的参数。

    In [14]: df = dask.datasets.timeseries()
    
    In [15]: def myfunc(x, arg1, arg2, arg3):
        ...:     return x.mean()
        ...:
    
    In [16]: df.groupby("id")[['x', 'y']].apply(myfunc, meta={'x': 'f8', 'y': 'f8'}, arg1=1, arg2=2, arg3=3)
    Out[16]:
    Dask DataFrame Structure:
                          x        y
    npartitions=30
                    float64  float64
                        ...      ...
    ...                 ...      ...
                        ...      ...
                        ...      ...
    Dask Name: myfunc, 303 tasks
    

    【讨论】:

    • 这解释了为什么一个参数有效,而两个参数无效。谢谢,我错了!
    猜你喜欢
    • 1970-01-01
    • 2022-08-20
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    相关资源
    最近更新 更多