
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding the Last Business Day of Every Month in a Year Using Python
Business days are known as working day in corporate sector, in some sector it is Monday to friday and in some they are Monday to Saturday. In this article we will understand how to find the last business day of every year using Python. Python offers various libraries like datetime, time, calendar etc to deal with the time operations. We will be utilizing those libraries to program the same. Additionally some libraries like Pandas also have in?built methods to support such time operations.
Using The Datetime And Calender Module
The 'datetime', and 'calendar' are standard Python modules to deal with the time. It offers several utility functions to deal with the time data. The definition of business days can vary depending upon several factors. Typically Monday to Friday is considered a business day.
Example
In the following example we used the 'monthrange' method to determine the number of days in the given month. The 'datetime.date' method on the other hand creates a date object to deal with the time operations. Next we used a while loop to keep decrementing the 'date' by one day till we encounter any weekday (from Monday till Friday). Finally we returned the 'date object.
import datetime import calendar def last_business_day(year, month): last_day = calendar.monthrange(year, month)[1] date = datetime.date(year, month, last_day) while date.weekday() > 4: date -= datetime.timedelta(days=1) return date year = 2023 month = 9 last_bd = last_business_day(year, month) print("The last business day of {} {} is {}.".format( calendar.month_name[month], year, last_bd))
Output
The last business day of September 2023 is 2023-09-29.
Using The Dateutils Library
'dateutil' library of Python offers several functionalities that go beyond the 'datetime' library of Python. It contains the 'rrule' module which allows us to work with the recurrent dates. This has a wide set of applications in data manipulation, generating sequence of dates etc. The 'relativedelta' class on the other hand allows us to perform operations on date time like addition, subtraction etc.
Example
In the following example we have used the datetime , dateutils, and the calendar module. We used the 'rrule' method of the 'dateutil' module to generate a recurrent rule which asks to be generated on a monthly basis. We specified Monday to Friday to be considered using the 'byweekly' parameter. We kept 'count=1' to get only one date element.
import datetime from dateutil import rrule, relativedelta import calendar def last_business_day(year, month): rule = rrule.rrule( rrule.MONTHLY, bymonth=month, bysetpos=-1, byweekday=(rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR), dtstart=datetime.datetime(year, month, 1), count=1 ) return rule[0].date() year = 2023 month = 4 last_bd = last_business_day(year, month) print("The last business day of {} {} is {}.".format( calendar.month_name[month], year, last_bd))
Output
The last business day of April 2023 is 2023-04-28.
Using Pandas Library
Pandas is a popular open source library of Python which was built to deal with data manipulation and analysis. It deals with the data frames which consist of rows and columns. In pandas we have several in?built methods. Few of these methods are 'Timestamp', 'Monthend', 'Dateoff' etc. We can utilize these methods to deal with the date and time operations.
Example
In the following example we have first imported the calendar and the pandas library. We used the 'Timestamp' method to create a date time object and the 'Monthend' method to get the last date of the month. Next we checked if the date falls under the business day category. If it is not we keep decrementing the day by one till we find a day that falls under the business day.
import calendar import pandas as pd def last_business_day(year, month): date = pd.Timestamp(year, month, 1) + pd.offsets.MonthEnd(0) while date.weekday() > 4: date -= pd.DateOffset(days=1) return date.date() year = 2023 month = 12 last_bd = last_business_day(year, month) print("The last business day of {} {} is {}.".format( calendar.month_name[month], year, last_bd))
Output
The last business day of December 2023 is 2023-12-29.
Conclusion
In this article we understood how to find the last business day of every month in a year using Python. We utilized the datetime, calendar libraries to perform the same. We used several utility functions like Timestamp, rrule etc. We also saw the usage of another popular library called Pandas to deal with the same task.