我正在尝试创建一个简单的时间序列,不同的滚动类型。一个具体的例子是使用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
有人能提出建议吗?
亲切的问候
答案 0 :(得分:0)
@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