Python TA Lib不适用于pandas系列

2024-04-20 07:52:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Ubuntu 12.04上使用python中的TA Lib作为described in the official documentation。但是,当使用pandasDataFrames或Series时,如不同来源上的多个示例所示,我得到以下TypeError

Traceback (most recent call last): File "test1.py", line 14, in analysis['rsi'] = ta.RSI(spy.Close) TypeError: Argument 'real' has incorrect type (expected numpy.ndarray, got Series)

执行本代码时:

import pandas.io.data as data
import pandas as pd
import talib as ta
import matplotlib.pyplot as plt

# Download SP500 data with pandas
spyidx = data.get_data_yahoo('SPY', '2013-01-01')
analysis = pd.DataFrame(index = spyidx.index)
analysis['rsi'] = ta.RSI(spyidx.Close)

出什么事了?


Tags: inimportpandasclosedataindexasanalysis
3条回答

首先需要使用抽象函数:

import talib.abstract as ta

而不是

import talib as ta

其次,确保使用正确的名称:

ta_serie = pd.DataFrame({
    'high': _1_minute_tf.max_price,
    'open': _1_minute_tf.open_price,
    'close': _1_minute_tf.close_price,
    'low': _1_minute_tf.min_price
})

最后,享受:ta.SAR(ta_serie, window)会给你你想要的。

试试看

analysis = pd.DataFrame(index = spyidx.index.values)

对于pandas>;=0.13.0:

Passing a Series directly to a cython function expecting an ndarray type will no long work directly, you must pass Series.values

因此,在TA-lib修改其API以适应较新的pandas版本之前,您需要使用Series.valuesDataFrame.values

相关问题 更多 >