Search code examples
pythonpandasdataframetechnical-indicator

TypeError: object of type 'numpy.float64' has no len()


I'm trying to calculate Money Flow Index for bitcoin price.

To do this I'm using gdax, pandas and pyti.

Here's my code:

import gdax
import pandas as pd
from pyti.money_flow_index import money_flow_index as mfi

public_client = gdax.PublicClient()
historic = public_client.get_product_historic_rates('BTC-USD', granularity=60)
pd.set_option('display.max_rows', 30)
df = pd.DataFrame(historic)
df.columns = ['Time', 'Low', 'High', 'Open', 'Close', 'Volume']
df = df.head(n=30)

print(df, '\n')
close_data = df['Close'][0]
high_data = df['High'][0]
low_data = df['Low'][0]
volume_data = df['Volume'][0]
period = 14
print(mfi(close_data, high_data, low_data, volume_data, period))

Here's the error I'm getting:

Traceback (most recent call last):
  File "tiiii.py", line 18, in <module>
    print(mfi(close_data, high_data, low_data, volume_data, period))
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyti\money_
flow_index.py", line 19, in money_flow_index
    close_data, high_data, low_data, volume
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyti\catch_
errors.py", line 26, in check_for_input_len_diff
    arrays_len = [len(arr) for arr in args]
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyti\catch_
errors.py", line 26, in <listcomp>
    arrays_len = [len(arr) for arr in args]
TypeError: object of type 'numpy.float64' has no len()

Edit: OK so now I'm using:

close_data = df['Close']
high_data = df['High']
low_data = df['Low']
volume_data = df['Volume']

And here's what I'm getting:

            0
0         NaN
1         NaN
2         NaN
3         NaN
4         NaN
5         NaN
6         NaN
..        ...
23  97.914228
24  97.816960
25  96.440309
26  94.668462
27  94.340548
28  91.255057
29  87.706573

[30 rows x 1 columns]

I don't understand the order of the values. Also why am I not getting the full list?

P.S. Thanks for the help Rahul and timgeb!


Solution

  • The function money_flow_index calls check_for_input_len_diff and throws an error on line 26 when it tries to accumulate the length of the arguments close_data, high_data, low_data, volume you provided to money_flow_index (imported as mfi).

    The comments indicate that these arguments are supposed to be data sets (or at least data structures with a length), but you provided a float number, which has no length.