'ValueError: Length of values does not match length of index (PYTHON)
I'm trying to implement the stochastic indicator in TA-Lib but I'm getting the error above. The error is on the last line. Please see code below:
import pandas_datareader as pdr
import datetime
import pandas as pd
import numpy as np
import talib as ta
#Download Data
aapl = pdr.get_data_yahoo('AAPL', start=datetime.datetime(2006, 10, 1), end=datetime.datetime(2012, 1, 1))
#Saves Data as CSV on desktop
aapl.to_csv('C:\\Users\\JDOG\\Desktop\\aapl_ohlc.csv', encoding='utf-8')
#Save to dataframe
df = pd.read_csv('C:\\Users\JDOG\\Desktop\\aapl_ohlc.csv', header=0, index_col='Date', parse_dates=True)
#Initialize the `signals` DataFrame with the `signal` column
signals = pd.DataFrame(index=aapl.index)
signals['signal'] = 0.0
#Create slow stochastics //**Broken**
signals['Slow Stochastics'] = ta.STOCH(aapl.High.values,aapl.Low.values,aapl.Close.values,fastk_period=5,slowk_period=3,slowk_matype=0,slowd_period=3,slowd_matype=0)
Solution 1:[1]
Your error is that the STOCH function returns a tuple and you are trying to add a tuple value to your dataframe. Try this:
thirtyyear['StochSlowk'],thirtyyear['StochSlowD'] = ta.STOCH(thirtyyear['High'].values, thirtyyear['Low'].values, thirtyyear['Close'].values, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Juan I Marquez |
