Python TA-Lib not working with pandas series

I am trying to use TA-Lib in python on Ubuntu 12.04 as described in the official documentation . However, when using pandas DataFrame or Series , as shown in several examples in different sources, I get the following TypeError :

Traceback (last last call): File "test1.py", line 14, to the analysis ['rsi'] = ta.RSI (spy.Close) TypeError: The argument "real" has the wrong type (expected numpy.ndarray, got Series )

When executed, for example, this code:

 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) 

Something went wrong?

+6
source share
4 answers

For pandas > = 0.13.0:

Passing the series directly to a cython function waiting for the ndarray type will not work for long, you must pass Series.values

Therefore, before TA-lib revises its API to accommodate new versions of pandas , you need to use Series.values or DataFrame.values .

+5
source

First you need to use abstract functions:

import talib.abstract as ta

instead

import talib as ta

Secondly, make sure you use the correct names:

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

Finally, enjoy: ta.SAR(ta_serie, window) will give you what you expected.

+2
source

try

 analysis = pd.DataFrame(index = spyidx.index.values) 
+1
source

As the error message indicates, TA-lib expects numpy.ndarray but spyidx.Close - Pandas series

Change this line

  analysis['rsi'] = ta.RSI(spyidx.Close) 

to:

  analysis['rsi'] = ta.RSI(np.array(spyidx.Close)) 
+1
source

All Articles