0

Using either python, pandas or dateutil, how do I get last friday of the three month period, where the period end could be april. Since I am targeting a "focal date", which is last friday of the months with a three month lead time, I may need to extend leadtime to six months.

SourceDate, Q1, Q2, Q3

2013-Jun-26 2013-Sep-20 2013-Aug-16 2013-Jul-19
2013-Jun-25 2013-Sep-20 2013-Aug-16 2013-Jul-19
2013-Jun-24 **2013-Sep-20** 2013-Aug-16 2013-Jul-19
**2013-Jun-21** **2013-Jun-21** 2013-Aug-16 2013-Jul-19
2013-Jun-20 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-19 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-18 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-17 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-14 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-13 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-12 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-11 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-10 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-07 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-06 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-05 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-04 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-Jun-03 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-31 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-30 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-29 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-28 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-24 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-23 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-22 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-21 2013-Jun-21 2013-Aug-16 2013-Jul-19
2013-May-20 2013-Jun-21 **2013-Aug-16** 2013-Jul-19
**2013-May-17** 2013-Jun-21 **2013-May-17** 2013-Jul-19
2013-May-16 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-15 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-14 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-13 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-10 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-09 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-08 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-07 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-06 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-03 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-02 2013-Jun-21 2013-May-17 2013-Jul-19
2013-May-01 2013-Jun-21 2013-May-17 2013-Jul-19
2013-Apr-30 2013-Jun-21 2013-May-17 2013-Jul-19
2013-Apr-29 2013-Jun-21 2013-May-17 2013-Jul-19
2013-Apr-26 2013-Jun-21 2013-May-17 2013-Jul-19
2013-Apr-25 2013-Jun-21 2013-May-17 2013-Jul-19
2013-Apr-24 2013-Jun-21 2013-May-17 2013-Jul-19
2013-Apr-23 2013-Jun-21 2013-May-17 2013-Jul-19
2013-Apr-22 2013-Jun-21 2013-May-17 **2013-Jul-19**
**2013-Apr-19** 2013-Jun-21 2013-May-17 **2013-Apr-19**
2013-Apr-18 2013-Jun-21 2013-May-17 2013-Apr-19
2013-Apr-17 2013-Jun-21 2013-May-17 2013-Apr-19
2013-Apr-16 2013-Jun-21 2013-May-17 2013-Apr-19
2013-Apr-15 2013-Jun-21 2013-May-17 2013-Apr-19
2013-Apr-12 2013-Jun-21 2013-May-17 2013-Apr-19
2013-Apr-11 2013-Jun-21 2013-May-17 2013-Apr-19
2013-Apr-10 2013-Jun-21 2013-May-17 2013-Apr-19
2013-Apr-09 2013-Jun-21 2013-May-17 2013-Apr-19

I have tried pandas BMonthEnd and BQuarterEnd with WeekOfMonth(weekday = 4, week =2), I cant the rolling part.

Edit:

>>>d = dt.datetime(2013, 5, 15)
>>>list(rrule(MONTHLY, count=5, byweekday=FR, bysetpos=3,dtstart =d ))
[datetime.datetime(2013, 5, 17, 0, 0),
 datetime.datetime(2013, 6, 21, 0, 0),
 datetime.datetime(2013, 7, 19, 0, 0),
 datetime.datetime(2013, 8, 16, 0, 0),
 datetime.datetime(2013, 9, 20, 0, 0)]
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Merlin
  • 24,552
  • 41
  • 131
  • 206

2 Answers2

3
>>> from dateutil.rrule import *
>>> list(rrule(MONTHLY, count=3, byweekday=FR, bysetpos=-1))
[datetime.datetime(2013, 7, 26, 22, 4, 20),
 datetime.datetime(2013, 8, 30, 22, 4, 20),
 datetime.datetime(2013, 9, 27, 22, 4, 20)]

>>> list(rrule(MONTHLY, count=12, byweekday=FR, bysetpos=-1))[::3]
[datetime.datetime(2013, 7, 26, 22, 15, 14),
 datetime.datetime(2013, 10, 25, 22, 15, 14),
 datetime.datetime(2014, 1, 31, 22, 15, 14),
 datetime.datetime(2014, 4, 25, 22, 15, 14)]

rrule

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 1
    Which now is the last Friday of a quarter (three-month period) the OP asks for? – Alfe Jul 03 '13 at 13:07
  • @falsetru, the output is wrong for 5/15/2013, the first date should be jun/21 not 5/17. – Merlin Jul 03 '13 at 14:09
  • @Merlin, Sorry, I don't understand what do you mean. My answer/output does not contain 'jun/21' nor 'may/27'. – falsetru Jul 03 '13 at 14:25
  • @Merlin, What is your expected output? – falsetru Jul 03 '13 at 15:38
  • @falsetru, see question look at the third fridays, the first column should be three month based on (3,6,9,12) the second col three month based on the month (based on (2,5,8,11), third column 3 month based on months (1,4,7,10)... But not quarter end but third friday of the current period. – Merlin Jul 03 '13 at 15:56
0

If anyone like me ends up here from google, I found this approach easier to read:

d = datetime.now()
import pandas.tseries.offsets as offsets
print(d + offsets.QuarterEnd() - offsets.Week(weekday=4))

pandas doc

oxdeadbeef
  • 160
  • 9