Friday, 27 November 2020

How to have pandas perform a rolling average on a non-uniform x-grid

I would like to perform a rolling average but with a window that only has a finite 'vision' in x. I would like something similar to what I have below, but I want a window range that based on the x value rather than position index.

While doing this within pandas is preferred numpy/scipy equivalents are also OK

import numpy as np 
import pandas as pd 

x_val = [1,2,4,8,16,32,64,128,256,512]
y_val = [x+np.random.random()*200 for x in x_val]

df = pd.DataFrame(data={'x':x_val,'y':y_val})
df.set_index('x', inplace=True)

df.plot()
df.rolling(1, win_type='gaussian').mean(std=2).plot()

So I would expect the first 5 values to be averaged together because they are within 10 xunits of each other, but the last values to be unchanged.



from How to have pandas perform a rolling average on a non-uniform x-grid

No comments:

Post a Comment