【问题标题】:'DataFrame' object is not callable in a ApplyResult reference'DataFrame' 对象在 ApplyResult 引用中不可调用
【发布时间】:2020-05-10 16:07:15
【问题描述】:

我想首先声明我知道此错误消息已多次发布。但我似乎无法理解这些帖子如何适用于我。所以我想试试运气:

我有数据框“df”,我正在尝试对该数据框的子集执行并行处理:

for i in range(1, 2):
    pool = ThreadPool(processes=4)
    async_result = pool.apply_async(helper.Helper.transform(df.copy(), i))
    lst.append(async_result)

results = []
for item in lst:
    currentitem = item.get()
    results.append(currentitem)

辅助方法:

@staticmethod
def transform(df, i):
    return df

所以我通常在 Java 中编写代码,对于一个类,我需要在 python 中做一些事情。我只是不明白为什么在这种情况下我会收到错误:

Traceback (most recent call last):
  File "C:/Users/Barry/file.py", line 28, in <module>
    currentitem = item.get()
  File "C:\Users\Barry\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\pool.py", line 768, in get
    raise self._value
  File "C:\Users\Barry\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
TypeError: 'DataFrame' object is not callable

在线程函数中或在创建线程之前打印会产生正确的输出。

【问题讨论】:

    标签: python pandas dataframe parallel-processing


    【解决方案1】:

    问题在于这一行:

    async_result = pool.apply_async(helper.Helper.transform(df.copy(), i))
    

    问题 - 您在将其传递给 apply_async 之前调用了函数“transform”。结果,apply async 接收到一个数据帧,“认为”它是一个函数,并尝试异步调用它。结果是您看到的异常,并且此结果保存为AsyncResult 对象的一部分。

    要修复它,只需将此行更改为:

    async_result = pool.apply_async(helper.Helper.transform, (df.copy(), i))
    

    注意apply_async 有两个参数——函数和函数的参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-23
      • 2018-01-08
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 2014-02-05
      • 1970-01-01
      相关资源
      最近更新 更多