I have a time series like this one:
value identifier
2007-01-01 0.781611 55
2007-01-01 0.766152 56
2007-01-01 0.766152 57
2007-01-03 0.733152 57
2007-01-12 0.766152 57
2007-02-01 0.705615 55
2007-02-01 0.032134 56
2007-02-07 0.132134 56
2007-02-01 0.032134 57
2008-01-01 0.026512 55
2008-01-01 0.993124 56
2008-01-01 0.993124 57
2008-01-11 0.226512 56
2008-02-01 0.226420 55
2008-02-01 0.033860 56
2008-02-01 0.033860 57
2008-02-14 0.333860 55
2008-02-24 0.334860 57
Now I want to calculate the differences with different time deltas. My first approach was to
last_date = '2008-02-30'
dt = datetime.timedelta(days=30)
delta_30d = datetime.datetime.strptime(last_date, "%Y-%m-%d") - dt
df.loc[last_date]['value'] - df.loc[delta_30d]['value']
but if the exact same date on the time delta doesn't exist, I get
'the label [2008-01-30 00:00:00] is not in the [index]'
So this is a no go. How can I get the closest date from the requested time delta and calculate the difference?