Home > Mobile >  Can't find length of Dataframe Error: Float object can't be called
Can't find length of Dataframe Error: Float object can't be called

Time:11-16

So I have a Pandas Dataframe and I am trying to find the length of said data frame to split it in half. However when using the code

half_df = len(df) // 2 I get the error: TypeError: 'float' object is not callable

I can't seem to get my head around the problem. Shouldn't the Pandas data frame be a Dataframe and not a float type ?

Complete code below:

df = pd.read_csv('dataframe.csv')
df

    Symbol  Description Category2   Category3   GICS Sector Market cap  Dividend yield  Price to TTM earnings   Price to TTM sales  Price to book value Action
0   AAPL    Apple Inc   Common stocks   Large cap   Information Technology  $2,381,445,264,600  0.61%   23.36   6.17    35.33   Analyze
1   MSFT    Microsoft Corp  Common stocks   Large cap   Information Technology  $1,842,074,858,614  1.00%   25.42   9.57    11.31   Analyze
2   GOOG    Alphabet Inc Class C    Common stocks   Large cap   Communication Services  $1,164,555,710,000  0.00%   7.90    4.31    2.32    Analyze
....

half_df = len(df) // 2

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-e20afcd61c48> in <module>
----> 1 half_df = len(xrz) // 2

TypeError: 'float' object is not callable

CodePudding user response:

It appears that you have redefined the builtin len() function, assigning the name len to a float. That is, you must have done something like this earlier in your code:

len = 5.0

When you later write len(df), Python tries to call the float that has been assigned to len, but floats are not callable, and so it raises an Exception.

This sort of bug is why it's a good idea to avoid re-defining builtins like len. A linter like pylint can help find such oversights.

  • Related