0

i Have a daterange

pd.bdate_range("2001-01-01", "2018-01-01")

and want to find the third business day of the month (ignore holidays for now). How do I do that?

freddy888
  • 956
  • 3
  • 18
  • 39

2 Answers2

1

As you are already in business dates, you could resample to the start of the business month ('BMS') and add an offset of 3 business days::

>>> pd.Series(index=pd.bdate_range("2001-01-01",
                    "2018-01-01")).resample('BMS').index + pd.datetools.BDay(3)

DatetimeIndex(['2001-01-04', '2001-02-06', '2001-03-06', '2001-04-05',
           '2001-05-04', '2001-06-06', '2001-07-05', '2001-08-06',
           '2001-09-06', '2001-10-04',
           ...
           '2017-04-06', '2017-05-04', '2017-06-06', '2017-07-06',
           '2017-08-04', '2017-09-06', '2017-10-05', '2017-11-06',
           '2017-12-06', '2018-01-04'],
          dtype='datetime64[ns]', length=205, freq=None)

You'll find further details on how to work with dates in pandas in the documentation.

Mabel Villalba
  • 2,538
  • 8
  • 19
0

Plan: groupby year, month. Choose third with nth().

This example will be easier with a series:

dates = pd.Series(pd.bdate_range("2001-01-01", "2018-01-01"))
dates.groupby((dates.dt.year, dates.dt.month)).nth(3)

Partial output:

2001 1 2001-01-04 2 2001-02-06 3 2001-03-06 4 2001-04-05 5 2001-05-04 6 2001-06-06 7 2001-07-05 8 2001-08-06 9 2001-09-06 10 2001-10-04 11 2001-11-06 12 2001-12-06 2002 1 2002-01-04 2 2002-02-06 3 2002-03-06 4 2002-04-04

cfort
  • 2,655
  • 1
  • 19
  • 29