Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas DataFrame.empty() gives TypeError: 'bool' object is not callable

My DataFrame pipeline needs to handle empty and malformed results, and I added a test df.empty() and encountered this error:

(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)

Clearly this example DF would return False, because it's not empty (I'll just test for only one row) but now I'm curious why I'm getting this error its not a bool.

like image 428
xtian Avatar asked Aug 31 '19 00:08

xtian


2 Answers

pandas.DataFrame.empty is not a callable method, but a property.

Just use it as tabledf.empty rather than tabledf.empty()

The error you are getting is due to the fact that what you are doing is akin to:

>>> 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
like image 55
foglerit Avatar answered Nov 15 '22 08:11

foglerit


empty is an attriibute, not a method, s remove the ()

df = pd.DataFrame()
print(df.empty)
# True

df=pd.DataFrame({"a": [1]})
print(df.empty)
# False
like image 32
Andrew Lavers Avatar answered Nov 15 '22 09:11

Andrew Lavers