我正在尝试从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)
答案 0 :(得分:0)
注意您的逗号(,
)。在python中,逗号通常读作元组。
因此,尽管您的代码有效,但这并没有实现您认为的目的:
text = "x={:.strftime('%Y-%m-%d)'},y={:} ",format(xmax, ymax)
您想做的是:
text = "x={:.strftime('%Y-%m-%d)'},y={:} ".format(xmax, ymax)
from datetime import datetime
ymax = 474.600006
texty= "y={:.3f}".format(ymax)
print(texty)
xmax= datetime.today()
textx= "x={:.strftime('%Y-%m-%d)}".format(xmax)
print(textx)
# Here you had `,`format(xpos, ymax)
text = "x={:.strftime('%Y-%m-%d)'},y={:} ".format(xmax, ymax)
print(text)
# result: "x=.strftime('2018-01-23)"