Python 2.7 ValueError:在中添加批注时,float()的文本无效

Python 2.7 ValueError:在中添加批注时,float()的文本无效,python-2.7,pandas,matplotlib,dataframe,jupyter,Python 2.7,Pandas,Matplotlib,Dataframe,Jupyter,当我尝试向绘图添加注释时,出现此错误-ValueError:invalid literal for float():10_May 我的数据帧: 我的代码(我在绘图之前使用对datetime和strftime进行排序,因为我需要对存储为字符串的日期进行排序): grouped.index[9]返回u'10\u May'而grouped['L'][9]返回10.0。 我知道pandas希望索引是浮动的,但我想我可以通过df.index[]访问它。非常感谢您的建议。对于我来说,先绘制图,然后通过以下

当我尝试向绘图添加注释时,出现此错误-
ValueError:invalid literal for float():10_May

我的数据帧:

我的代码(我在绘图之前使用
对datetime
strftime
进行排序,因为我需要对存储为字符串的日期进行排序):

grouped.index[9]
返回
u'10\u May'
grouped['L'][9]
返回
10.0

我知道pandas希望索引是浮动的,但我想我可以通过df.index[]访问它。非常感谢您的建议。

对于我来说,先绘制图,然后通过以下方式获得索引位置:

样本:

np.random.seed(10)
df = pd.DataFrame({'L':[3,5,0,1]}, index=['4_May','3_May','1_May', '2_May'])
#print (df)
df.index = pd.to_datetime(df.index, format='%d_%b')
df = df.sort_index()
df.index = df.index.strftime('%d_%b')
df.plot()
plt.annotate('Peak',
             (df.index.get_loc(df.index[2]), df['L'][2]),
             xytext=(15, 15), 
             textcoords='offset points',
             arrowprops=dict(arrowstyle='-|>'))

编辑:

使用+++更通用的解决方案:


谢谢,周末愉快!
ax = df.plot()
ax.annotate('Peak',
             (df.index.get_loc(df.index[9]), df['L'][9]),
             xytext=(15, 15), 
             textcoords='offset points',
             arrowprops=dict(arrowstyle='-|>'))
np.random.seed(10)
df = pd.DataFrame({'L':[3,5,0,1]}, index=['4_May','3_May','1_May', '2_May'])
#print (df)
df.index = pd.to_datetime(df.index, format='%d_%b')
df = df.sort_index()
df.index = df.index.strftime('%d_%b')
df.plot()
plt.annotate('Peak',
             (df.index.get_loc(df.index[2]), df['L'][2]),
             xytext=(15, 15), 
             textcoords='offset points',
             arrowprops=dict(arrowstyle='-|>'))
ax = df.plot()
ax.annotate('Peak',
             (df.index.get_loc(df['L'].idxmax()), df['L'].max()),
             xytext=(15, 15), 
             textcoords='offset points',
             arrowprops=dict(arrowstyle='-|>'))