首页 > 解决方案 > Pandas DataFrame.empty() 给出 TypeError: 'bool' object is not callable

问题描述

我的 DataFrame 管道需要处理空的和格式错误的结果,我添加了一个测试df.empty()并遇到了这个错误:

(Pdb) isinstance(tabledf, pd.DataFrame)
True
(Pdb) tabledf.empty()
*** TypeError: 'bool' object is not callable
(Pdb) tabledf
  From Location  Account Description  Value        TableName
0  NaN      NaN         nan       TOTAL       0  countreport
(Pdb) tabledf.shape
(1, 6)

显然这个例子 DF 会返回False,因为它不是空的(我只会测试一行)但现在我很好奇为什么我得到这个错误它不是bool.

标签: python-3.xpandas

解决方案


pandas.DataFrame.empty不是可调用的方法,而是属性。

只是使用它tabledf.empty而不是tabledf.empty()

您得到的错误是由于您正在做的事情类似于:

>>> some_boolean = True
>>> some_boolean()

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-02ece9c024ce> in <module>
      1 boolean = False
----> 2 boolean()

TypeError: 'bool' object is not callable

推荐阅读