I find myself lost trying to solve this problem (automating tax paperwork). I have two dataframes: one with the quarterly historical records of EUR/USD exchange rates, and another with my own invoices, as an example:
import pandas as pd
import numpy as np
usdeur = [(pd.Timestamp('20170705'),1.1329),
(pd.Timestamp('20170706'),1.1385),
(pd.Timestamp('20170707'),1.1412),
(pd.Timestamp('20170710'),1.1387),
(pd.Timestamp('20170711'),1.1405),
(pd.Timestamp('20170712'),1.1449)]
labels = ['Date', 'Rate']
rates = pd.DataFrame.from_records(usdeur, columns=labels)
transactions = [(pd.Timestamp('20170706'), 'PayPal', 'USD', 100, 1),
(pd.Timestamp('20170706'), 'Fastspring', 'USD', 200, 1),
(pd.Timestamp('20170709'), 'Fastspring', 'USD', 100, 1),
(pd.Timestamp('20170710'), 'EU', 'EUR', 100, 1),
(pd.Timestamp('20170710'), 'PayPal', 'USD', 200, 1)]
labels = ['Date', 'From', 'Currency', 'Amount', 'Rate']
sales =pd.DataFrame.from_records(transactions, columns=labels)
resulting in:
I would need to have the sales['Rate']
column filled with the proper exchange rates from the rates['Rate']
, that is to say:
- if
sales['Currency']
is'EUR'
, leave it alone. - for each row of
sales
, find the row inrates
with matching'Date'
; grab that veryrates['Rate']
value and put it insales['Rate']
- bonus: if there's no matching
'Date'
(e.g. during holidays, the exchange market is closed), check the previous row until a suitable value is found.
The full result should look like the following (note that row #2 has the rate from 2017-07-07):
I've tried to follow several suggested solutions from other questions, but with no luck. Thank you very much in advance