Pandas DataFrame.empty() 给出 TypeError: 'bool' 对象不可调用

xti*_*ian 5 python-3.x pandas

我的 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)
Run Code Online (Sandbox Code Playgroud)

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

fog*_*rit 5

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
Run Code Online (Sandbox Code Playgroud)