finding last business day of a month in python finding last business day of a month in python python python

finding last business day of a month in python


I use the following:

from pandas.tseries.offsets import BMonthEndfrom datetime import dated=date.today()offset = BMonthEnd()#Last day of current monthoffset.rollforward(d)#Last day of previous monthoffset.rollback(d)


Let's say you want to get the last business days of the month up-to the end of the next two years, the following will work.

   import pandas as pd   import datetime   start = datetime.date.today()   end = datetime.date(start.year+2, 12, 31)   bussiness_days_rng =pd.date_range(start, end, freq='BM')


For one-liner fans:

import calendardef last_business_day_in_month(year: int, month: int) -> int:    return max(calendar.monthcalendar(year, month)[-1:][0][:5])