Python 从Alphavantage api读取数据时,我无法访问日期列
我正在尝试使用AlphaVantage API将数据导入dataframe并在Plotly dash中使用。我能看到数据,但不能在绘图中读取日期列。 请帮忙Python 从Alphavantage api读取数据时,我无法访问日期列,python,pandas,dataframe,plotly,plotly-dash,Python,Pandas,Dataframe,Plotly,Plotly Dash,我正在尝试使用AlphaVantage API将数据导入dataframe并在Plotly dash中使用。我能看到数据,但不能在绘图中读取日期列。 请帮忙 import pandas as pd import configparser from alpha_vantage.timeseries import TimeSeries import matplotlib.pyplot as plt import plotly.graph_objs as go import plotly.expre
import pandas as pd
import configparser
from alpha_vantage.timeseries import TimeSeries
import matplotlib.pyplot as plt
import plotly.graph_objs as go
import plotly.express as px
import datetime
parser = configparser.RawConfigParser()
parser.read('config_key.ini')
info = parser['apikey']['key']
ts = TimeSeries(key=info,output_format='pandas', indexing_type='date')
data, meta_data = ts.get_daily(symbol='AAPL', outputsize='full')
data.rename(columns={'date':'Date','1. open': 'open', '2. high': 'high',
'3. low': 'low', '4. close': 'close', '5. volume': 'volume'},
inplace=True)
data.sort_values(by='date', ascending=False, inplace=True)
data.head()
[enter image description here][1]
获取的数据帧的日期已编制索引,因此它将是
data.index
,而不是data['date']
。因为我没有使用配置解析器,所以代码改变了我们获取API信息的方式
import pandas as pd
from alpha_vantage.timeseries import TimeSeries
import matplotlib.pyplot as plt
import datetime
with open('./alpha_vantage_api_key.txt') as f:
api_key = f.read()
ts = TimeSeries(key=api_key, output_format='pandas', indexing_type='date')
data, meta_data = ts.get_daily(symbol='AAPL', outputsize='full')
data.rename(columns={'date':'Date','1. open': 'open', '2. high': 'high',
'3. low': 'low', '4. close': 'close', '5. volume': 'volume'},
inplace=True)
data.sort_values(by='date', ascending=False, inplace=True)
data.head()
open high low close volume
date
2020-11-20 118.64 118.7700 117.290 117.34 73604287.0
2020-11-19 117.59 119.0600 116.810 118.64 74112972.0
2020-11-18 118.61 119.8200 118.000 118.03 76322111.0
2020-11-17 119.55 120.6741 118.960 119.39 74270973.0
2020-11-16 118.92 120.9900 118.146 120.30 91183018.0
import plotly.graph_objects as go
fig = go.Figure(data=[go.Candlestick(x=data.index,
open=data['open'],
high=data['high'],
low=data['low'],
close=data['close'])])
fig.show()
采集的数据帧的日期被索引,因此它将是
数据。索引
而不是数据['date']
。因为我没有使用配置解析器,所以代码改变了我们获取API信息的方式
import pandas as pd
from alpha_vantage.timeseries import TimeSeries
import matplotlib.pyplot as plt
import datetime
with open('./alpha_vantage_api_key.txt') as f:
api_key = f.read()
ts = TimeSeries(key=api_key, output_format='pandas', indexing_type='date')
data, meta_data = ts.get_daily(symbol='AAPL', outputsize='full')
data.rename(columns={'date':'Date','1. open': 'open', '2. high': 'high',
'3. low': 'low', '4. close': 'close', '5. volume': 'volume'},
inplace=True)
data.sort_values(by='date', ascending=False, inplace=True)
data.head()
open high low close volume
date
2020-11-20 118.64 118.7700 117.290 117.34 73604287.0
2020-11-19 117.59 119.0600 116.810 118.64 74112972.0
2020-11-18 118.61 119.8200 118.000 118.03 76322111.0
2020-11-17 119.55 120.6741 118.960 119.39 74270973.0
2020-11-16 118.92 120.9900 118.146 120.30 91183018.0
import plotly.graph_objects as go
fig = go.Figure(data=[go.Candlestick(x=data.index,
open=data['open'],
high=data['high'],
low=data['low'],
close=data['close'])])
fig.show()