应用于滚动 window 函数会丢失列名

apply to a rolling window function loses column names

我有一个接受 dataframe:

的函数
def max_dd(df):
    print(df)

    return None

如果我 print df.head() 在将它传递给 max_dd 之前,它看起来像这样:

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 列名,我该如何取回它?

Rolling.apply func 接收的不是 DataFrame 作为参数,而是 Series:

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 Series