我有一个接受dataframe参数的函数
def max_dd(df):
print(df)
return None如果我在传递给max_dd之前使用print df.head(),它看起来是这样的:
print(df.head())
Close
Date
2010-08-10 7.95
2010-08-11 7.67
2010-08-12 7.72
2010-08-13 7.64
2010-08-16 7.59但是,如果我现在将df传递给max_dd,
new = df.rolling(45).apply(max_dd)该函数打印:
Date
2010-08-10 7.95
2010-08-11 7.67
2010-08-12 7.72
2010-08-13 7.64
2010-08-16 7.59为什么它丢失了Close列名,如何找回它?
发布于 2021-08-31 21:58:06
Rolling.apply函数接收的不是DataFrame作为参数,而是一个序列:
def max_dd(df):
print(df)
print(type(df))
return None
new = df.rolling(45).apply(max_dd)输出:
Date
2010-08-10 7.95
2010-08-11 7.67
2010-08-12 7.72
2010-08-13 7.64
2010-08-16 7.59
dtype: float64
<class 'pandas.core.series.Series'> # <- Not a DataFrame but a Serieshttps://stackoverflow.com/questions/69005495
复制相似问题