pandas - 绘制来自 Alpha Vantage 的熊猫数据
问题描述
除了 pandas,我刚刚安装了 alpha_vantage 模块,并获得了 api-key 等。我现在想绘制有关股票的 som 数据。写在模块自述文件中(见这里)这是你如何做到的:
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()
推荐阅读
- python - Python 语法:函数参数中的 `for`
- centos - 在 Centos 问题上使用 yum 安装 GeoIP 包
- java - 这是一个有效的单例实现吗?
- php - 如何将嵌套的 json 数组转换为 $row 以插入数据库?
- python - 从一个网页中的其他选项卡获取内容
- c# - 自定义编辑器中的表情符号
- c++ - C++ 线程库异常
- python - 如何使用 Selenium 和 Python 在隐藏元素中上传图像
- logback - 如何在 logback.xml 中定义一些变量并能够在我的 java 代码中使用该变量?
- c++ - 尝试将 boost::stacktrace 添加到 CMake 项目时生成错误