How to get a daterange of the 2nd Fridays of each month? How to get a daterange of the 2nd Fridays of each month? pandas pandas

How to get a daterange of the 2nd Fridays of each month?


You can do this easily by setting freq='WOM-2FRI' ("week of month, second Friday") in pd.date_range. So to get your expected output, you could write:

pd.date_range('2016-01-01', freq='WOM-2FRI', periods=13)

The output is:

DatetimeIndex(['2016-01-08', '2016-02-12', '2016-03-11', '2016-04-08',               '2016-05-13', '2016-06-10', '2016-07-08', '2016-08-12',               '2016-09-09', '2016-10-14', '2016-11-11', '2016-12-09',               '2017-01-13'],              dtype='datetime64[ns]', freq='WOM-2FRI')


try this aproach:

import dateutil as duimport pandas as pdstart=du.parser.parse('2016-01-01')rr = du.rrule.rrule(du.rrule.MONTHLY,                    byweekday=du.relativedelta.FR(2),                    dtstart=start,                    count=12)dates = [pd.to_datetime(d) for d in rr]

Output:

In [33]: datesOut[33]:[Timestamp('2016-01-08 00:00:00'), Timestamp('2016-02-12 00:00:00'), Timestamp('2016-03-11 00:00:00'), Timestamp('2016-04-08 00:00:00'), Timestamp('2016-05-13 00:00:00'), Timestamp('2016-06-10 00:00:00'), Timestamp('2016-07-08 00:00:00'), Timestamp('2016-08-12 00:00:00'), Timestamp('2016-09-09 00:00:00'), Timestamp('2016-10-14 00:00:00'), Timestamp('2016-11-11 00:00:00'), Timestamp('2016-12-09 00:00:00')]