Pandas Resampling With Custom Volume Weighted Aggregation
I'm trying to do a volume weighted price aggregation based on a 5 second timestep for which I have multiple datapoints. I can get simple mean and sum aggregations for individual fi
Solution 1:
Using .apply
you can write any custom aggregation function you want.
def vwap(data):
return (data.P * data.Q).sum() / data.Q.sum()
When using a grouper, you can apply it like this:
df.groupby(pd.Grouper(freq="5s")).apply(vwap)
With resampling, .apply
can be used as well:
df.resample("5s").apply(vwap)
Post a Comment for "Pandas Resampling With Custom Volume Weighted Aggregation"