简体   繁体   English

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

[英]Python Pandas rolling mean DataFrame Constructor not properly called

I am trying to create a simple time-series, of different rolling types. 我正在尝试创建一个不同滚动类型的简单时间序列。 One specific example, is a rolling mean of N periods using the Panda python package. 一个特定的示例是使用Panda python软件包的N个周期的滚动平均值。

I get the following error : ValueError: DataFrame constructor not properly called! 我收到以下错误:ValueError:DataFrame构造函数未正确调用!

Below is my code : 下面是我的代码:

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

Would anyone be able to advise? 有人可以提供建议吗?

Kind regards 亲切的问候

found the correction! 找到了更正! It was erroring out (new error), where I had to explicitly declare n as an integer. 这是错误的(新错误),在这里我必须显式地将n声明为整数。 Below, the code works 下面的代码有效

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM