现在看看雅虎财务的替代品似乎终于好转了。遇到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
没有任何运气,任何帮助都会非常感激!
答案 0 :(得分:2)
很明显你的索引是一个字符串。使用df.index.astype
:
In [1165]: df.index = df.index.astype(np.datetime64)
现在,你正在做的事情会起作用:
In [1166]: (df.index[-1] - df.index[0]).days
Out[1166]: 4