在python中对绘图进行注释时出现KeyError
我有以下代码:在python中对绘图进行注释时出现KeyError,python,python-3.x,pandas,datetime,annotations,Python,Python 3.x,Pandas,Datetime,Annotations,我有以下代码: import pandas as pd import datetime as date from pandas_datareader import data as web from pandas.tseries.offsets import * import matplotlib as mpl import matplotlib.pyplot as plt import mplcursors start = date.datetime(2018,1,1) end = date
import pandas as pd
import datetime as date
from pandas_datareader import data as web
from pandas.tseries.offsets import *
import matplotlib as mpl
import matplotlib.pyplot as plt
import mplcursors
start = date.datetime(2018,1,1)
end = date.datetime.today()
xl_file = pd.read_excel(r'C:\Users\user\randomdates.xlsx')
xl_file.set_index("Date", inplace = True)
xl_file.index = pd.to_datetime(xl_file.index, format='%Y-%m-%d')
stock = 'BTC-USD'
data = web.DataReader(stock, 'yahoo', start, end)
fig , ax = plt.subplots(figsize=(18, 4))
data.plot(y='Close', ax = ax)
newdates = xl_file.loc[start:end]
for anotate in (xl_file.index):
ax.annotate(anotate, xy=(anotate, data['Close'].loc[anotate]), xycoords='data',
xytext=(-30, 40), textcoords='offset points',
size=13, ha='center', va="baseline",
bbox=dict(boxstyle="round", alpha=0.1),
arrowprops=dict(arrowstyle="wedge,tail_width=0.5", alpha=0.1));
我提到的excel文件中有以下条目:
日期是随机的,我用以下excel公式得到这些日期:
=RANDBETWEEN(DATE(2018, 1, 1),DATE(2019, 10, 20))
出于某种原因,该图是为pandas\u数据读取器
生成的,但它不是在图上注释
。我不断遇到以下错误,不确定如何处理:
KeyError: 1514764800000000000
请告知如何解决此错误。尝试使用Pandas内置的处理日期的方法,名为
Pandas.to_datetime()
,该方法将您的列转换为日期格式
这里有一个很好的参考: