本人小白,高手进来帮我解释下这是为什么 | python | python 技术论坛-江南app体育官方入口
ser = pd.series([1, 2, 1, 3, 5, 6, 1, 6, 6])
print(ser)
def func(s):
n = s.index[0]
df_choice = ser[:n 1][ser[:n 1] == ser[:n 1][-1:].values[0]]
if len(df_choice)>1:
m = df_choice.tail(2).index[1]-df_choice.tail(2).index[0]
else:
m=’不显示’
return m
print(ser.rolling(1).apply(func))
为什么当自定义函数的else的m值为字符时会有出错信息:typeerror: must be real number, not str
当我把m=’不显示’改成m=123 时就运行成功了
返回类型不一致
,所以报错.