0

我正在尝试创建一个不同滚动类型的简单时间序列。一个具体的例子是使用 Panda python 包的 N 个周期的滚动平均值。

我收到以下错误:ValueError:DataFrame 构造函数未正确调用!

下面是我的代码:

def py_TA_MA(v, n, AscendType):  
    df = pd.DataFrame(v, columns=['Close'])
    df = df.sort_index(ascending=AscendType) # ascending/descending flag
    M = pd.Series(df['Close'].rolling(n), name = 'MovingAverage_' + str(n))
    df = df.join(M)
    df = df.sort_index(ascending=True) #need to double-check this
    return df

有人可以建议吗?

亲切的问候

4

1 回答 1

0

找到更正了!它出错了(新错误),我必须明确地将 n 声明为整数。下面,代码有效

@xw.func
@xw.arg('n', numbers = int, doc = 'this is the rolling window')
@xw.ret(expand='table')     
def py_TA_MA(v, n, AscendType):  
   df = pd.DataFrame(v, columns=['Close'])
   df = df.sort_index(ascending=AscendType) # ascending/descending flag
   M = pd.Series(df['Close'], name = 'Moving Average').rolling(window = n).mean()
   #df = pd.Series(df['Close']).rolling(window = n).mean()
   df = df.join(M)
   df = df.sort_index(ascending=True) #need to double-check this
return df
于 2018-05-30T15:46:46.880 回答