I have a dataframe consisting of 30 time series of opening stock prices taken from the Dow Jones index:
source=pd.read_html('https://en.wikipedia.org/wiki/Dow_Jones_Industrial_Average')
df = pd.DataFrame(source[1])
tickers_symbols=df['Symbol'].values.tolist()
dataset = pd.DataFrame()
for t in tickers_symbols:
tmp = yf.download(t, period='5y', progress=False)
tmp.reset_index(inplace=True)
tmp['Ticker'] = t
dataset = dataset.append(tmp, ignore_index=True)
dataset=dataset.drop(["Close", "High", "Low", "Adj Close", "Volume"], axis=1)
dataset = dataset.pivot_table(index="Date", columns="Ticker", values="Open")
What I want to do is use the function below to retrieve four diagnostic plots for each stock and save them in a specific folder.
import statsmodels.tsa.api as smt
def tsplot2(y, title, lags=None, figsize=(12,8)):
fig= plt.figure(figsize=figsize)
layout=(2,2)
ts_ax=plt.subplot2grid(layout, (0,0))
hist_ax=plt.subplot2grid(layout, (0,1))
acf_ax=plt.subplot2grid(layout, (1,0))
pacf_ax=plt.subplot2grid(layout, (1,1))
y.plot(ax=ts_ax)
ts_ax.set_title(title, fontsize=14, fontweight='bold')
y.plot(ax=hist_ax, kind='hist', bins=25)
hist_ax.set_title('Histogram')
smt.graphics.plot_acf(y, lags=lags, ax=acf_ax)
smt.graphics.plot_pacf(y, lags=lags, ax=pacf_ax)
sns.despine()
plt.tight_layout()
return ts_ax, acf_ax, pacf_ax
The way I tried is the following:
for column in dataset:
tsplot2(dataset[column], title=dataset[column], lags=50)
plt.savefig(f"{figures}/" column "Plots.png", format="png")
However, when I run the code I get the following error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I am not sure what this means in this context. Is there a way to fix this loop?
CodePudding user response:
You made a mistake in the function call:
tsplot2(dataset[column], title=dataset[column], lags=50)
should be
tsplot2(dataset[column], title=column, lags=50)