Premium problem131. First Purchase, Last Purchase, Days Between

Medium Locked

purchases has columns customer_id and purchase_date (datetime).

Return a DataFrame with columns customer_id, first_purchase, last_purchase and active_days, the whole number of days between the two as an integer. A customer who bought once has 0 active days. Sort by customer_id and renumber the index from 0.

Input

   customer_id purchase_date
0            1    2022-01-01
1            1    2022-03-02
2            1    2022-02-01
3            2    2022-05-05

Output

   customer_id first_purchase last_purchase  active_days
0            1     2022-01-01    2022-03-02           60
1            2     2022-05-05    2022-05-05            0

Premium problem

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

Implement solve(...)