0

I am trying to make a function to calculate rolling mean or sum :

def rolling_lag_creator(dataset,i,func):  
    dataset['Bid']=dataset.loc[:,'Bid'].rolling(window=i).func()
    return dataset

but this function is throwing error when i am calling this:

rolling_lag_creator(Hypothesis_ADS,5,mean)

Error:

NameError: name 'mean' is not defined

while below code works fine:

dataset['Bid']=dataset.loc[:,'Bid'].rolling(window=5).mean()

Can anyone help me how to call such methods as parameters, thank you.

2
  • The error message is telling you “name ‘mean’ is not defined’. You need to define the function mean. Or refer to an existing function mean() Commented May 25, 2020 at 19:39
  • Rolling function return is rolling object and is not working Commented May 25, 2020 at 19:53

2 Answers 2

1

mean is a method, not a function per se. And anyway, attribute access doesn't work like that, but you can use getattr, something like this:

def rolling_lag_creator(dataset, i, method_name):
    method = getattr(dataset.loc[:,'Bid'].rolling(window=i), method_name)
    dataset['Bid'] = method()
    return dataset

rolling_lag_creator(Hypothesis_ADS, 5, 'mean')
Sign up to request clarification or add additional context in comments.

1 Comment

Waao , thanks your answer works flawlessly , this is what i needed thank you so much
0

I guess you need to:

import numpy as np

and then replace mean with np.mean

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.