Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

slice pandas timeseries on date +/- 2 business days

having following timeseries:

In [65]: p
Out[65]: 
Date
2008-06-02    125.20
2008-06-03    124.47
2008-06-04    124.40
2008-06-05    126.89
2008-06-06    122.84
2008-06-09    123.14
2008-06-10    122.53
2008-06-11    120.73
2008-06-12    121.19
Name: SPY

how can I slice on a specfic date +/- 2 neighbouring (business) days, so ie if d = '2008-06-06':

 -2   2008-06-04    124.40
 -1   2008-06-05    126.89
  0   2008-06-06    122.84
  1   2008-06-09    123.14
  2   2008-06-10    122.53
like image 940
ronnydw Avatar asked Dec 27 '22 12:12

ronnydw


1 Answers

Pandas has some pretty nice business day functionality built in that will handle this automatically. For this exact problem, it actually ends up being a bit more code, but it will handle a much more general case very easily.

In [1]: ind = pd.date_range('2008-06-02', '2008-06-12', freq='B')

In [2]: p = pd.Series(np.random.random(len(ind)), index=ind)

In [3]: p
Out[3]:
2008-06-02    0.606132
2008-06-03    0.328327
2008-06-04    0.842873
2008-06-05    0.272547
2008-06-06    0.013640
2008-06-09    0.357935
2008-06-10    0.517029
2008-06-11    0.992851
2008-06-12    0.053158
Freq: B, dtype: float64

In [4]: t0 = pd.Timestamp('2008-6-6')

In [5]: from pandas.tseries import offsets

In [6]: delta = offsets.BDay(2)

This will create a two business day offset. You can also make arbitrary offsets of other time units, or even combinations of time units. Now with the starting point and delta, you can slice intelligently in the standard way:

In [7]: p[t0 - delta:t0 + delta]
Out[7]:
2008-06-04    0.842873
2008-06-05    0.272547
2008-06-06    0.013640
2008-06-09    0.357935
2008-06-10    0.517029
Freq: B, dtype: float64

The nice thing about this approach is that the interval is not linked to the number of rows. So, for instance, if you had hourly data and maybe some missing points, you could still capture two business days exactly the same way. Or if your data source happened to have weekend data in it as well but you still wanted +/- 2 business days.

like image 155
user2543645 Avatar answered Feb 03 '23 09:02

user2543645