Premium problem143. Carry Balances Forward Within a Customer

Medium Locked

balances has columns customer_id, date (datetime) and balance, with gaps where nothing was recorded.

Sorted by date within each customer, fill each missing balance with that customer's most recent known one. A customer's leading gap, with nothing before it, stays missing, and no value may cross from one customer to the next.

Return the original columns sorted by customer_id then date, with the index renumbered from 0.

Input

   customer_id       date  balance
0            1 2022-01-01     10.0
1            1 2022-01-02      NaN
2            1 2022-01-03      NaN
3            2 2022-01-01      NaN
4            2 2022-01-02      5.0

Output

   customer_id       date  balance
0            1 2022-01-01     10.0
1            1 2022-01-02     10.0
2            1 2022-01-03     10.0
3            2 2022-01-01      NaN
4            2 2022-01-02      5.0

Premium problem

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

Implement solve(...)