[英]Python Pandas rolling mean DataFrame Constructor not properly called
我正在嘗試創建一個不同滾動類型的簡單時間序列。 一個特定的示例是使用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
有人可以提供建議嗎?
親切的問候
找到了更正! 這是錯誤的(新錯誤),在這里我必須顯式地將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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.