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?