I have a data frame with the following columns: {'day','measurement'}
And there might be several measurements in a day (or no measurements at all)
For example:
day | measurement
1 | 20.1
1 | 20.9
3 | 19.2
4 | 20.0
4 | 20.2
and an array of coefficients:
coef={-1:0.2, 0:0.6, 1:0.2}
My goal is to resample the data and average it using the coefficiets, (missing data should be left out).
This is the code I wrote to calculate that
window=[-1,0,-1]
df['resampled_measurement'][df['day']==d]=[coef[i]*df['measurement'][df['day']==d-i].mean() for i in window if df['measurement'][df['day']==d-i].shape[0]>0].sum()
df['resampled_measurement'][df['day']==d]/=[coef[i] for i in window if df['measurement'][df['day']==d-i].shape[0]>0].sum()
For the example above, the output should be:
Day measurement
1 20.500
2 19.850
3 19.425
4 19.875
The problem is that the code runs forever, and I'm pretty sure that there's a better way to resample with coefficients.
Any advice would be highly appreciated !