从 Alpha Vantage 绘制熊猫数据

问题描述

我刚刚安装了 alpha_vantage 模块和 pandas,并获得了 api-key 等。我现在想绘制有关股票的一些数据。 写在模块自述文件 (see here) 中,您是这样操作的:

from alpha_vantage.timeseries import TimeSeries

import matplotlib.pyplot as plt

ts = TimeSeries(key='YOUR_API_KEY',output_format='pandas')
data,Meta_data = ts.get_inTraday(symbol='MSFT',interval='1min',outputsize='full')
data['4. close'].plot()
plt.title('InTraday Times Series for the MSFT stock (1 min)')
plt.show()

但是当我在我的项目中使用自己的 api-key 编写相同的代码时,我收到此错误

  File "C:\Users\augbi\PycharmProjects\Djangoprosjekt\main\views.py",line 159,in <module>
  data['4. close'].plot()
  TypeError: tuple indices must be integers or slices,not str

我打印了数据,所以你可以在这里看到格式(最上面一行是data.index的结果:

index: <built-in method index of tuple object at 0x0F7CBC28>
(                     1. open  2. high   3. low  4. close  5. volume
date
2021-01-04 19:55:00  3179.94  3183.45  3179.94   3183.45     1317.0
2021-01-04 19:50:00  3179.00  3180.00  3178.26   3178.26      851.0
2021-01-04 18:52:00  3178.11  3178.11  3178.11   3178.11      648.0
2021-01-04 18:15:00  3177.00  3177.00  3177.00   3177.00      505.0
2021-01-04 18:09:00  3177.00  3177.00  3177.00   3177.00      224.0
...                      ...      ...      ...       ...        ...
2020-12-22 07:40:00  3212.78  3212.78  3212.78   3212.78      703.0
2020-12-22 07:34:00  3210.00  3210.00  3210.00   3210.00      755.0
2020-12-22 07:27:00  3208.19  3208.19  3208.19   3208.19      510.0
2020-12-22 07:14:00  3204.00  3204.00  3204.00   3204.00      216.0
2020-12-22 07:08:00  3204.00  3204.00  3204.00   3204.00      167.0

我自己的代码在这里

ts2 = TimeSeries(key='ALPA_KEY',output_format='pandas')
data = ts2.get_inTraday(symbol='AMZN',outputsize='full')
print("index:",data.index)
print(data)
data['4. close'].plot()
plt.title('InTraday Times Series for the AMZN stock (1 min)')
plt.show()

我的愿望是绘制“4. Closing”列,如果可能的话,绘制相应的时间。此数据代表亚马逊 1 天的股价。

在此先非常感谢您!

解决方法

我有一个 Alpah-Vantage APIKEY,所以我用下面的代码来检查。它没有按照您的要求返回错误。查了一下输出格式,不是普通的pandas格式,而是扩展格式。

data
(                     1. open  2. high   3. low  4. close  5. volume
 date                                                               
 2021-01-04 19:55:00  3179.94  3183.45  3179.94   3183.45     1317.0
 2021-01-04 19:50:00  3179.00  3180.00  3178.26   3178.26      851.0
 2021-01-04 18:52:00  3178.11  3178.11  3178.11   3178.11      648.0
 2021-01-04 18:15:00  3177.00  3177.00  3177.00   3177.00      505.0
 2021-01-04 18:09:00  3177.00  3177.00  3177.00   3177.00      224.0
 ...                      ...      ...      ...       ...        ...
 2020-12-22 07:40:00  3212.78  3212.78  3212.78   3212.78      703.0
 2020-12-22 07:34:00  3210.00  3210.00  3210.00   3210.00      755.0
 2020-12-22 07:27:00  3208.19  3208.19  3208.19   3208.19      510.0
 2020-12-22 07:14:00  3204.00  3204.00  3204.00   3204.00      216.0
 2020-12-22 07:08:00  3204.00  3204.00  3204.00   3204.00      167.0
 
 [3440 rows x 5 columns],{'1. Information': 'Intraday (1min) open,high,low,close prices and volume','2. Symbol': 'AMZN','3. Last Refreshed': '2021-01-04 19:55:00','4. Interval': '1min','5. Output Size': 'Full size','6. Time Zone': 'US/Eastern'})

从这种格式,可以得到一个普通的数据框,data[0],这样就可以创建一个图了。以下是获取图表的代码。

data[0]
    1. open     2. high     3. low  4. close    5. volume
date                    
2021-01-04 19:55:00     3179.94     3183.45     3179.94     3183.45     1317.0
2021-01-04 19:50:00     3179.00     3180.00     3178.26     3178.26     851.0
2021-01-04 18:52:00     3178.11     3178.11     3178.11     3178.11     648.0
2021-01-04 18:15:00     3177.00     3177.00     3177.00     3177.00     505.0
2021-01-04 18:09:00     3177.00     3177.00     3177.00     3177.00     224.0

ts2 = TimeSeries(key=api_key,output_format='pandas')
data = ts2.get_intraday(symbol='AMZN',interval='1min',outputsize='full')
print("index:",data.index)
print(data)
data[0]['4. close'].plot()
plt.title('Intraday Times Series for the AMZN stock (1 min)')
plt.show()

enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...