1

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?

Ivan
  • 19,560
  • 31
  • 97
  • 141

1 Answers1

1

If your index was unique, you could use DateTimeIndex.asof, as shown in this question.

Another way (from that same question) would be to use numpy.argmin to find the closest date. For example, you could do

In [138]: import numpy as np

In [139]: last_date = pd.Timestamp('2008-01-30')

In [140]: delta_30d = last_date - pd.Timedelta(30, 'D')

In [142]: np.argmin(np.abs(df.index - delta_30d))
Out[142]: 13

In [144]: df.iloc[13, :]
Out[144]: 
value          0.22642
identifier    55.00000
Name: 2008-02-01 00:00:00, dtype: float64

In [145]: df.iloc[np.argmin(np.abs(df.index - last_date))]['value'] - df.iloc[np.argmin(np.abs(df.index - delta_30))]['value']
Out[145]: 0.19990799999999997
Community
  • 1
  • 1
chrisb
  • 49,833
  • 8
  • 70
  • 70