Assuming you have a datetime
column like the following, we can use pandas.bdate_range
to check which dates are business days, then use Series.cumsum
to get the cumulative sum:
df = pd.DataFrame({'date': pd.date_range('2020-04-01', '2020-04-07')})
date
0 2020-04-01
1 2020-04-02
2 2020-04-03
3 2020-04-04
4 2020-04-05
5 2020-04-06
6 2020-04-07
Solution
# get date range of business days
business_days = pd.bdate_range(df['date'].min(), df['date'].max())
# check which dates are business days
bdays_bool = df['date'].isin(business_days)
# get the cumulative sums of all business days per month
df['nth_business_day'] = bdays_bool.groupby([df['date'].dt.year, df['date'].dt.month]).cumsum()
date nth_business_day
0 2020-03-29 0.0
1 2020-03-30 1.0
2 2020-03-31 2.0
3 2020-04-01 1.0
4 2020-04-02 2.0
5 2020-04-03 3.0
6 2020-04-04 3.0
7 2020-04-05 3.0
8 2020-04-06 4.0
9 2020-04-07 5.0