Python Alpha Vantage(Yahoo Fin alt)和;熊猫数据帧问题-。天
现在,雅虎金融似乎已经一去不返了。偶然发现的,看起来很有趣。下面的代码正在运行,直到我尝试查找开始日期和结束日期之间的增量,但我得到以下错误:Python Alpha Vantage(Yahoo Fin alt)和;熊猫数据帧问题-。天,python,python-3.x,pandas,dataframe,alpha-vantage,Python,Python 3.x,Pandas,Dataframe,Alpha Vantage,现在,雅虎金融似乎已经一去不返了。偶然发现的,看起来很有趣。下面的代码正在运行,直到我尝试查找开始日期和结束日期之间的增量,但我得到以下错误: from alpha_vantage.timeseries import TimeSeries ts = TimeSeries(key=key, output_format="pandas") data, meta_data = ts.get_daily("AAPL",outputsize="full") days = (data.index[-1]-
from alpha_vantage.timeseries import TimeSeries
ts = TimeSeries(key=key, output_format="pandas")
data, meta_data = ts.get_daily("AAPL",outputsize="full")
days = (data.index[-1]- data.index[0]).days
输出:
days = (data.index[-1]- data.index[0]).days
TypeError: unsupported operand type(s) for -: 'str' and 'str'
我也试过:
days = (float(data.index[-1])- float(data.index[0])).days
及
打印数据帧头,如下所示
print(data.head())
输出:
open low close high volume
2000-01-03 104.87 101.69 111.94 112.50 4783900.0
2000-01-04 108.25 101.19 102.50 110.62 4574800.0
2000-01-05 103.75 103.00 104.00 110.56 6949300.0
2000-01-06 106.12 95.00 95.00 107.00 6856900.0
2000-01-07 96.50 95.50 99.50 101.00 4113700.0
没有任何运气,任何帮助都将不胜感激 很明显,您的索引是一个字符串。使用
df.index.astype
将其转换为datetime,如下所示:
In [1165]: df.index = df.index.astype(np.datetime64)
现在,您所做的将起作用:
In [1166]: (df.index[-1] - df.index[0]).days
Out[1166]: 4
尝试:
(data.index[-1].astype(np.datetime64)-data.index[0].astype(np.datetime64)).days
此外,发布数据帧片段也会有所帮助。@COLDSPEED不幸的是,我以前也尝试过,但没有成功。获取以下错误,“AttributeError:'str'对象没有属性'astype'”。我还添加了数据帧的头部,只是为了进一步说明。
In [1166]: (df.index[-1] - df.index[0]).days
Out[1166]: 4