1 Year Rolling mean pandas on column date
I believe this should work for you:
# First make sure that `date` is a datetime object:df['date'] = pd.to_datetime(df['date'])df.set_index('date').groupby('id').rolling(window=1, freq='A').mean()['variation']
using pd.DataFrame.rolling
with datetime works well when the date
is the index, which is why I used df.set_index('date')
(as can be seen in one of the documentation's examples)
I can't really test if it works on the year's average on your example dataframe, as there is only one year and only one ID, but it should work.
Arguably Better Solution:
[EDIT] As pointed out by Mihai-Andrei Dinculescu, freq
is now a deprecated argument. Here is an alternative (and probably more future-proof) way to do what you're looking for:
df.set_index('date').groupby('id')['variation'].resample('A').mean()
You can take a look at the resample
documentation for more details on how this works, and this link regarding the frequency arguments.