我试图从yahoo finance中提取最大收盘价及其对应的日期,并在图表上注释该点。代码如下所示。你知道吗
ymax = tencents[['Close']].max()
ymax
#result: Close 474.600006
dtype: float64
texty= "y={:.3f}".format(ymax)
texty
#result: unsupported format string passed to Series.__format__
#expected: 474.60
xmax= tencents['Close'].idxmax()
#result: "x=.strftime('2018-01-23)"
textx= "x={:.strftime('%Y-%m-%d)}".format(xpos)
textx
result: "x=.strftime('2018-01-23)"
当我把两者结合起来:
text = "x={:.strftime('%Y-%m-%d)},y={:} ",format(xpos,ymax)
text
#result: format() argument 2 must be str, not Series
#expected: (2018-01-23, 474.60)
注意逗号(
,
)。在python中,逗号通常被解读为元组。你知道吗因此,尽管您的代码是有效的,但它并不像您认为的那样:
text = "x={:.strftime('%Y-%m-%d)'},y={:} ",format(xmax, ymax)
你想做的是:
text = "x={:.strftime('%Y-%m-%d)'},y={:} ".format(xmax, ymax)
相关问题 更多 >
编程相关推荐