绘制 pandas-来自 Alpha Vantage 的数据
Plot pandas-data from Alpha Vantage
除了 pandas 之外,我刚刚安装了 alpha_vantage 模块,并获得了 api-key 等。我现在想绘制有关股票的 som 数据。
写在模块自述文件 (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',interval='1min', 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()
除了 pandas 之外,我刚刚安装了 alpha_vantage 模块,并获得了 api-key 等。我现在想绘制有关股票的 som 数据。 写在模块自述文件 (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',interval='1min', 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()