How to get a centred rolling mean?

Viewed 1215

I want to compute the rolling mean of data taken on successive days. If I just use dataframe.rolling(7) the mean is from the previous week. Instead I would like day to be at the centre of the window the mean is computed over not right at the end. Is there an easy/fast way to get such a centred rolling mean of a pandas Series?

2 Answers
df.rolling(7, center=True).mean()

The rolling mean can be centered by setting the center argument to True.

Example plot

df.shift(-4).rolling(7).mean()

Should do the trick.

Related