Skip to content
Advertisement

how to get last 10 business days if

I want to create dataframe based on last 10 business days. Also it should check whether the day is public holiday or not. I have a list of public holiday.

List of public holiday is:

Holiday
2021-01-26
2021-03-11
2021-03-29
2021-04-02
2021-04-14
2021-04-21
2021-05-13
2021-07-21
2021-08-19
2021-09-10
2021-10-15
2021-11-04
2021-11-05
2021-11-19

weekends saturday and sunday.

so i run the code today, which is saturday 27th Feb 2021, than output should be like this

Business days
2021-02-15
2021-02-16
2021-02-17
2021-02-18
2021-02-19
2021-02-22
2021-02-23
2021-02-24
2021-02-25
2021-02-26

Advertisement

Answer

Alternative to @pi_pascal:

hols = ["2021-01-26", "2021-03-11", "2021-03-29", "2021-04-02",
        "2021-04-14", "2021-04-21", "2021-05-13", "2021-07-21",
        "2021-08-19", "2021-09-10", "2021-10-15", "2021-11-04",
        "2021-11-05", "2021-11-19"]
hols = pd.to_datetime(hols)

bdays = pd.bdate_range(end=pd.Timestamp.today(), periods=60, freq="1D", closed="left")
bdays = bdays[bdays.weekday < 5].difference(hols)[-10:]
>>> bdays
DatetimeIndex(['2021-02-15', '2021-02-16', '2021-02-17', '2021-02-18',
               '2021-02-19', '2021-02-22', '2021-02-23', '2021-02-24',
               '2021-02-25', '2021-02-26'],
              dtype='datetime64[ns]', freq=None)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement