I'm working with OptionMetrics data, and I want to check my calculation of forward prices on the SPX index. The problem I'm having is that the rates information I've been given is discrete, so I have to provide an interpolation for each calculation. With apply, this is horifically inefficient.
To be precise:
I have a dataframe of forward prices, F. F contains columns date, expiration, and forwardprice. date and expiration are datetime.date types, and forwardprice has floats. My information on rates is contained in a separate dataframe, r. On a specific date, r contains a forward curve on that date, i.e.:
r[r['date'] == example_date] will yield something like:
pd.DataFrame({'date': [dt.date(2022, 1, 3), dt.date(2022, 1, 3)], 'days': [10, 20], 'rate': [0.05, 0.06]})
On each date in F, I need to calculate the days to expiration (I can do this), and then find the appropriate rate from the forward curve by linear interpolation. (In the above example, this means on Jan 3rd, 2022, my forward price for Jan 18 (+15 days) should come out to 0.055).
The only way I could think about doing this was:
F['rate'] = F.apply(lambda x: np.interp((x.expiration - x.date).days, r.loc[r['date'] == x.date, 'days'], r.loc[r['date'] == x.date, 'rate']), axis=1)
This approach is not practical for the size of the dataframes I'm working with, so I was curious to see if there's a better way. How can I make this more efficient? Do I need to approach the problem from a different way entirely? Thanks.