2017-07-25 26 views
0

寻找替代方案,现在雅虎财务似乎终于好转了。来到Alpha Vantage看起来很有趣。下面的代码运行,直到我试图找到开始和结束日期之间的差距,但我得到的错误如下:Alpha Vantage(Yahoo Fin alt)&Pandas Dataframe Issue - .days

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

尝试:'(data.index [-1] .astype(np.datetime64) - data.index [0] .astype(np.datetime64))。days' –

+0

此外,张贴您的数据框的片段将有所帮助。 –

+0

@COLDSPEED不幸的是,我已经尝试过,没有成功。获取以下错误:“AttributeError:'str'对象没有属性'astype'”我还添加了数据框的头部以便给出更多的说明。 – vlepore

回答

2

很明显你的索引是一个字符串。将它转换使用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