0

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.

I get the following error : ValueError: DataFrame constructor not properly called!

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

cs95
  • 379,657
  • 97
  • 704
  • 746
Kiann
  • 531
  • 1
  • 6
  • 20
  • What is `v`? The error most likely comes from `v` being something that `pandas.DataFrame()` is not designed to accept. – Peter Leimbigler May 25 '18 at 16:29
  • Hi Peter, v is the data that is fed in. I am pretty sure 'v' is fine. I specify 'v' upstream and use it in other dataframe functions, and those functions work. – Kiann May 27 '18 at 20:02
  • Could you post the output of `type(v)`, and also the exact contents if possible? The error message suggests to me that `v` is somehow malformed such that the `pd.DataFrame` line throws an error. Also maybe try without the `columns=['Close']` argument. – Peter Leimbigler May 27 '18 at 21:18

1 Answers1

0

found the correction! It was erroring out (new error), where I had to explicitly declare n as an integer. 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
Kiann
  • 531
  • 1
  • 6
  • 20