Premium problem157. Seven-Day Trailing Average per Store

Medium Locked

sales has columns store, date (datetime) and amount, at most one row per store and day but with days missing entirely.

Add a rolling_7d column: the mean of that store's amounts over the trailing seven calendar days, the current day included. Because days are missing, the window is a length of time rather than a count of rows, so a store with three rows in a week averages those three.

Return the original columns plus rolling_7d, sorted by store then date, with the index renumbered from 0. Windows must not reach across stores.

Input

  store       date  amount
0     A 2022-01-01    10.0
1     A 2022-01-03    20.0
2     A 2022-01-20    90.0
3     B 2022-01-01     5.0
4     B 2022-01-02    15.0

Output

  store       date  amount  rolling_7d
0     A 2022-01-01    10.0        10.0
1     A 2022-01-03    20.0        15.0
2     A 2022-01-20    90.0        90.0
3     B 2022-01-01     5.0         5.0
4     B 2022-01-02    15.0        10.0

Premium problem

This one's part of Premium. Unlock the full Pandas track plus every other premium problem on the site.

Implement solve(...)