Follow-up Rolling_apply Deprecated
Following up on this answer: Is there a way to do a weight-average rolling sum over a grouping? rsum = pd.rolling_apply(g.values,p,lambda x: np.nansum(w*x),min_periods=p) rolling
Solution 1:
As of 0.18+, use Series.rolling.apply
.
w = np.array([0.1,0.1,0.2,0.6])
df.groupby('ID').VALUE.apply(
lambda x: x.rolling(window=4).apply(lambda x: np.dot(x, w), raw=False))0NaN1NaN2NaN3146.04166.05NaN6NaN7NaN82.59NaN10NaN11NaN1235.51321.414NaN15NaN16NaN178.3189.819NaN
Name: VALUE, dtype: float64
The raw
argument is new to 0.23 (set it to specify passing Series v/s arrays), so remove it if you're having trouble on older versions.
Post a Comment for "Follow-up Rolling_apply Deprecated"