Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python TA-Lib not working with pandas series

Tags:

python

numpy

I am trying to use the TA-Lib in python on Ubuntu 12.04 as described in the official documentation. However, when using pandas DataFrames or Series, as shown in multiple examples on different sources, I get the following 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)

When executing e.g. 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)

What went wrong?

like image 772
user3257161 Avatar asked Jan 31 '14 11:01

user3257161


2 Answers

For 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

So before TA-lib revises its API to accommodate the newer pandas versions, you need to use Series.values or DataFrame.values.

like image 188
MLister Avatar answered Oct 02 '22 23:10

MLister


First you need to use abstract functions:

import talib.abstract as ta

instead of

import talib as ta

Second make sure you use 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.

like image 26
Julien Kieffer Avatar answered Oct 02 '22 21:10

Julien Kieffer