finding last business day of a month in python

30,382

Solution 1

I use the following:

from pandas.tseries.offsets import BMonthEnd
from datetime import date

d=date.today()

offset = BMonthEnd()

#Last day of current month
offset.rollforward(d)

#Last day of previous month
offset.rollback(d)

Solution 2

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')

Solution 3

For one-liner fans:

import calendar

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

Solution 4

I use this for the first business day of the month but it can be used for last business day of the month as well:

import time
import datetime
from pandas.tseries.holiday import USFederalHolidayCalendar
from pandas.tseries.offsets import CustomBusinessDay
from dateutil.relativedelta import relativedelta

#Create dates needed to be entered as parameters
today = datetime.date.today()
first = today.replace(day=1)
#End of the Prior Month
eopm = first - datetime.timedelta(days=1)
eopm = eopm.strftime("%Y%m%d")
#Create first business day of current month date
us_bd = CustomBusinessDay(calendar=USFederalHolidayCalendar())
focm = first
nxtMo = today + relativedelta(months=+1)
fonm = nxtMo.replace(day=1)
eocm = fonm - datetime.timedelta(days=1)
first_bd = pd.DatetimeIndex(start = focm, end = eocm, freq= us_bd)
first_bd = first_bd.strftime("%Y%m%d")
#First Business Day of the Month
first_bd = first_bd[0]
#Last Business Day of the Month
lst_day = len(first_bd)-1
last_bd = first_bd[lst_day]

I left some code in there that is not needed for the last business day of the current month, but may be useful to someone.

Share:
30,382
tkyass
Author by

tkyass

Updated on July 27, 2022

Comments

  • tkyass
    tkyass almost 2 years

    I'm trying to find last business day of of the month. I wrote the code below for that and it works fine but I was wondering if there is a cleaner way of doing it?

    from  datetime import date,timedelta
    import datetime
    import calendar
    
    today=datetime.date.today()
    
    last = today.replace(day=calendar.monthrange(today.year,today.month)[1])
    
    if last.weekday()<5:
        print last
    
    else:
        print last-timedelta(days=1+last.weekday()-5)
    

    Thanks in advance!

  • JohnAndrews
    JohnAndrews over 5 years
    How would you go about with a custom calendar? Like European business days in germany?
  • Elke
    Elke over 5 years
    You can define your own business month with the pandas offset function CustomBusinessMonthEnd. You can find documentation here: pandas.pydata.org/pandas-docs/stable/reference/api/…
  • xtheking
    xtheking almost 4 years
    This way does not seem to work ... AttributeError: 'function' object has no attribute 'monthcalendar'. An edit has been proposed.
  • Tobias Bruckert
    Tobias Bruckert about 2 years
    @xtheking works for me with python 3.8
  • resnbl
    resnbl about 2 years
    @xtheking: Under what conditions is [-1:][0] different than [-1]? I think it could be shorter/clearer...