Premium problem136. Convert Sales Using a Dated Rate Table

Medium Locked

sales has columns sale_id, currency, date (datetime) and amount. rates has columns currency, date (datetime) and rate, giving the USD value of one unit of that currency on that day.

Return a DataFrame with columns sale_id and amount_usd, keeping every sale and leaving amount_usd as NaN where no rate exists for that currency and date. Sort by sale_id and renumber the index from 0.

The key is the pair, not either column alone.

Input

sales =
   sale_id currency       date  amount
0        1      EUR 2022-01-03   100.0
1        2      GBP 2022-01-03   100.0
2        3      EUR 2022-01-04    50.0
rates =
  currency       date  rate
0      EUR 2022-01-03   1.1
1      GBP 2022-01-03   1.3

Output

   sale_id  amount_usd
0        1       110.0
1        2       130.0
2        3         NaN

Premium problem

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

Implement solve(...)