Adventures in Machine Learning

Mastering Business Days in Pandas: Excluding Weekends and Federal Holidays

Getting Business Days in Pandas: Excluding Weekends and Federal Holidays

In the business world, time is money, and every second counts. Businesses need to keep track of time to schedule meetings, plan projects, and make important decisions.

However, not all days are created equal, and some days are off-limits for business operations. Weekends and federal holidays are two examples of non-business days that companies need to take into account.

Thankfully, pandas, a popular library for data analysis in Python, provides easy-to-use methods for getting business days. In this article, we’ll explore two common methods to exclude weekends and federal holidays from the list of business days, and show some examples of how to use them.

Method 1: Excluding Weekends

The first method is to exclude weekends from the list of business days. Pandas provides the bdate_range() method to generate a range of dates that exclude weekends.

The bdate_range() method takes three arguments: start, end, and freq. The start and end arguments specify the date range, and the freq argument specifies the frequency of the range.

By default, the frequency is “B”, which stands for business day, and excludes weekends.

For example, let’s generate a list of business days from January 1, 2022, to January 10, 2022.

Here’s the code:

“`

import pandas as pd

start_date = ‘2022-01-01’

end_date = ‘2022-01-10’

business_days = pd.bdate_range(start_date, end_date)

“`

In this example, we imported the pandas library, then specified the start and end dates as strings. Then, we called the bdate_range() method with the start and end dates as arguments.

The method returned a DatetimeIndex object, which is a list of dates that exclude weekends.

We can print out the resulting business_days list to see the output:

“`

print(business_days)

“`

The output would be:

“`

DatetimeIndex([‘2022-01-03’, ‘2022-01-04’, ‘2022-01-05’, ‘2022-01-06’,

‘2022-01-07’],

dtype=’datetime64[ns]’, freq=’B’)

“`

As you can see, the list only contains the business days from January 3, 2022, to January 7, 2022.

Method 2: Excluding Weekends and Federal Holidays

The second method is to exclude both weekends and federal holidays from the list of business days.

Pandas provides the USFederalHolidayCalendar class and CustomBusinessDay class to handle federal holidays.

First, we need to import the required classes from pandas.tseries.holiday module:

“`

from pandas.tseries.holiday import USFederalHolidayCalendar

from pandas.tseries.offsets import CustomBusinessDay

“`

Then, we create a USFederalHolidayCalendar object to represent the list of US federal holidays:

“`

cal = USFederalHolidayCalendar()

“`

Now, we define a CustomBusinessDay object to represent a custom business day with the following parameters:

“`

business_day = CustomBusinessDay(calendar=cal)

“`

This tells Pandas to use the USFederalHolidayCalendar object to exclude federal holidays from the list of business days.

Finally, we call the date_range() method with the start and end dates as arguments, and set the freq argument to the custom business day:

“`

start_date = ‘2022-01-01’

end_date = ‘2022-01-10’

business_days = pd.date_range(start=start_date, end=end_date, freq=business_day)

“`

Here’s the complete code:

“`

from pandas.tseries.holiday import USFederalHolidayCalendar

from pandas.tseries.offsets import CustomBusinessDay

import pandas as pd

cal = USFederalHolidayCalendar()

business_day = CustomBusinessDay(calendar=cal)

start_date = ‘2022-01-01’

end_date = ‘2022-01-10’

business_days = pd.date_range(start=start_date, end=end_date, freq=business_day)

print(business_days)

“`

In this example, we first imported the required modules, then created a USFederalHolidayCalendar object and a CustomBusinessDay object. Then, we specified the start and end dates, and called the date_range() method with the CustomBusinessDay object as freq argument.

The resulting list of business_days would exclude weekends and federal holidays, which are January 1 (New Year’s Day) and January 3 (Martin Luther King Jr. Day) in this case.

Conclusion

In this article, we’ve explored two methods to get business days in Pandas, excluding weekends and federal holidays. By using the bdate_range() method or the CustomBusinessDay class, businesses can easily generate a list of business days to plan their operations.

Whether you need to schedule a meeting, launch a project, or make an important decision, Pandas provides a convenient way to handle time in the business world. Example 2: Excluding Weekends and Federal Holidays

In this example, we will use the CustomBusinessDay and USFederalHolidayCalendar classes to generate a list of business days that exclude both weekends and federal holidays using pandas.

First, let’s import the required libraries:

“`

import pandas as pd

from pandas.tseries.offsets import CustomBusinessDay

from pandas.tseries.holiday import USFederalHolidayCalendar

“`

Next, we create a USFederalHolidayCalendar object to represent the list of US federal holidays:

“`

cal = USFederalHolidayCalendar()

“`

Then, we create a CustomBusinessDay object with the USFederalHolidayCalendar instance as the input value for the calendar parameter:

“`

us_bus = CustomBusinessDay(calendar=cal)

“`

Now, let’s generate a list of business days for a date range using the date_range() method:

“`

start_date = ‘2022-01-01’

end_date = ‘2022-01-15’

us_business_days = pd.date_range(start=start_date, end=end_date, freq=us_bus)

“`

In this example, we specified the start date as January 1, 2022, and the end date as January 15, 2022. We used the us_bus CustomBusinessDay object as the frequency argument to the date_range() method to exclude weekends and federal holidays from the final list.

“`

print(us_business_days)

“`

The output would be:

“`

DatetimeIndex([‘2022-01-03’, ‘2022-01-04’, ‘2022-01-05’, ‘2022-01-06’,

‘2022-01-07’, ‘2022-01-10’, ‘2022-01-11’, ‘2022-01-12’,

‘2022-01-13’, ‘2022-01-14’],

dtype=’datetime64[ns]’, freq=’C’)

“`

As you can see, the resulting list only includes business days from January 3, 2022, to January 14, 2022, as both January 1 and January 17 are weekends, and January 3 and January 20 are federal holidays.

Additional Resources

If you want to learn more about working with business days in pandas, there are plenty of resources available. The pandas documentation covers the date offset classes in great detail, including CustomBusinessDay and all of its parameters.

The documentation also provides examples of how to create custom holiday calendars, which can be useful for businesses that operate in countries other than the United States.

In addition to the documentation, there are several online tutorials and courses that cover working with time series data in pandas, including business days.

For example, DataCamp has a course called “Working with Dates and Times in Python,” which covers date offsets, calendars, and more.

Conclusion

Working with business days in pandas can be a crucial part of many businesses’ operations. By using the CustomBusinessDay and USFederalHolidayCalendar classes, businesses can exclude both weekends and federal holidays from the list of business days, ensuring that they are making decisions based on the most accurate and up-to-date information.

Pandas provides a lot of flexibility when it comes to working with business days, with a range of classes and methods to suit different use cases. To get started, consult the pandas documentation or explore one of the many tutorials or courses available online.

With a little bit of practice, you’ll be generating custom business day lists like a pro. In conclusion, working with business days is a crucial part of many businesses’ operations.

The pandas library provides easy-to-use classes and methods for generating custom business day lists that exclude weekends and federal holidays. Two common methods, using the bdate_range() method or the CustomBusinessDay class with the USFederalHolidayCalendar, were explored in this article.

These methods allow businesses to make decisions based on the most accurate and up-to-date information. By taking advantage of pandas’ flexibility in working with business days, businesses can streamline their operations and increase efficiency.

With a little bit of practice, generating custom business day lists will become second nature to any business using pandas.

Popular Posts