TypeError:'numpy.float64'类型的对象没有len()

时间:2018-03-17 11:43:16

标签: python pandas dataframe technical-indicator

我正在尝试计算比特币价格的资金流量指数。

要做到这一点,我正在使用gdax,pandas和pyti。

这是我的代码:

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

这是我得到的错误:

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

修改 好的,现在我正在使用:

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

这就是我得到的:

            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]

我不明白价值观的顺序。另外,为什么我没有得到完整的清单?

P.S。感谢Rahul和timgeb的帮助!

2 个答案:

答案 0 :(得分:0)

我从pyti模块中提取了函数。这就是你的代码在模块中实际执行的方式。

def money_flow_index (close_data, high_data, low_data, volume, period):
    check_for_input_len_diff(
        close_data, high_data, low_data, volume
    )

def check_for_input_len_diff(*args):
    arrays_len = [len(arr) for arr in args]

print(
    money_flow_index(
        close_data, high_data,
        low_data, volume_data, period
    )
)

所以money_flow_index需要每个参数为array。您正在提供numpy.float64

答案 1 :(得分:0)

函数money_flow_index调用check_for_input_len_diff并在第26行尝试累计您提供给close_data, high_data, low_data, volume的参数money_flow_index的长度时抛出错误(导入为{ {1}})。

注释表明这些参数应该是数据集(或者至少是具有长度的数据结构),但是你提供了一个没有长度的浮点数。