简体   繁体   English

Python:滚动关联图导致“ ValueError:无法将字符串转换为float:”

[英]Python: Rolling Correlation Plot Results in “ValueError: could not convert string to float:”

I am trying to plot the rolling correlation of two stock prices. 我试图绘制两个股票价格的滚动相关性。 This function used to work but for some reason, it now is not working. 该功能曾经可以使用,但是由于某种原因,现在无法使用。 I am getting the an error when trying to plot using pd.rolling_corr 尝试使用pd.rolling_corr进行绘图时出现错误

For example, I download AAPL and TSLA stock data, and then get their adjusted prices into two series. 例如,我下载AAPL和TSLA股票数据,然后将其调整后的价格分为两个系列。 I then try to run a rolling correlation plot. 然后,我尝试运行滚动相关图。 I am getting the following error: 我收到以下错误:

"ValueError: could not convert string to float: '2013-01-02" “ ValueError:无法将字符串转换为浮点数:'2013-01-02”

import numpy as np
randn = np.random.randn
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.pylab as pylab
%matplotlib inline
from yahoo_finance import Share

start = '2013-01-01'
end = '2015-01-01'
TSLA=pd.DataFrame(Share('TSLA').get_historical(start, end))
TSLA=TSLA.set_index('Date')
AAPL=pd.DataFrame(Share('AAPL').get_historical(start, end))
AAPL=AAPL.set_index('Date')

TSLA_px=TSLA['Adj_Close']
AAPL_px=AAPL['Adj_Close']

rolling_correlation = pd.rolling_corr(TSLA_px, AAPL_px, 60)
plt.plot(rolling_correlation)
plt.xlabel('Day')
plt.ylabel('60-day Rolling Correlation')

愚蠢的菜鸟错误... yahoo数据中的date以字符串形式出现,需要转换为日期。

TSLA['Date']=pd.to_datetime(TSLA['Date'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM