Premium problem103. Three-Month Rolling Revenue

Hard Locked

purchases has columns user_id, created_at (datetime) and purchase_amount. Refunds appear as negative amounts and are to be ignored entirely.

Total the remaining revenue by calendar month and return a DataFrame with columns month (the month as a "YYYY-MM" string), revenue and rolling_3m, the mean of that month and the two before it. The first two months average only the months that exist rather than producing NaN. Sort by month and renumber the index from 0.

Input

   user_id created_at  purchase_amount
0        1 2021-01-05            100.0
1        2 2021-02-03            200.0
2        3 2021-03-09            300.0
3        4 2021-04-01            400.0
4        5 2021-04-20            -50.0
5        6 2021-02-14             50.0

Output

     month  revenue  rolling_3m
0  2021-01    100.0  100.000000
1  2021-02    250.0  175.000000
2  2021-03    300.0  216.666667
3  2021-04    400.0  316.666667

Premium problem

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

Implement solve(...)