Python Pandas滚动意味着DataFrame构造函数未正确调用

时间:2018-05-25 15:59:08

标签: python pandas dataframe rolling-average

我正在尝试创建一个简单的时间序列,不同的滚动类型。一个具体的例子是使用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

有人能提出建议吗?

亲切的问候

1 个答案:

答案 0 :(得分: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