Applying lambda function to a pandas rolling window series

10k views Asked by At

I have a function which takes an array and a value, and returns a value. I would like to apply it to my Series s on a rolling basis, so the array is always the rolling window. Here's a minimal example of what I've tried (unsuccessfully), using np.random.choice in place of my real function. I find lots of examples for finding rolling means and other built-in functions, but can't get it to work for my arbitrary lambda function.

s = pd.Series([1,2,3,4,5,6,7,8,9])
rolling_window = s.rolling(3)

First attempt:

new_values = s.apply(lambda x: np.random.choice(rolling_window, size=1)) 

ValueError: a [a is the first positional param of choice(), so refers to 'rolling_window'] must be 1-dimensional or an integer

Another attempt:

new_values = rolling_window.apply(lambda x: np.random.choice(size=1))

TypeError: choice() takes at least 1 positional argument (0 given)

...How do I apply an arbitrary lambda function (taking an array and a value) on each value in my Series, on each rolling-array window in my Series?

1

There are 1 answers

1
this be Shiva On BEST ANSWER

IIUC, if all you want to do is apply a function on the window, your second attempt comes close:

rolling_window.apply(lambda x: np.random.choice(x, size=1))

However, you can circumvent the use of the lambda like this:

rolling_window.apply(np.random.choice, kwargs={'size' : 1})

0    NaN
1    NaN
2    1.0
3    4.0
4    4.0
5    5.0
6    7.0
7    7.0
8    8.0
dtype: float64

Additional arguments to the function you pass go in args and kwargs.