Alpha Vantage (Yahoo Fin alt) & Pandas 数据框问题 - .days

Alpha Vantage (Yahoo Fin alt) & Pandas Dataframe Issue - .days

雅虎金融似乎终于一去不复返了,现在正在寻找替代方案。遇到 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]- 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

days = (int(data.index[-1])- int(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:

将其转换为日期时间
In [1165]: df.index = df.index.astype(np.datetime64)

现在,您所做的将起作用:

In [1166]: (df.index[-1] - df.index[0]).days
Out[1166]: 4