Python | Pandas tseries.offsets.CustomBusinessDay.onOffset

Python | Pandas tseries.offsets.CustomBusinessDay.onOffset

The pandas.tseries.offsets.CustomBusinessDay is an offset class that provides flexible business day computations. It can be used to represent a frequency that is a custom business day and can take into account weekends and custom holidays.

The onOffset() method associated with this (and many other offsets in pandas) is a method to check if a given date is on a "business day" as per the rules defined by the CustomBusinessDay.

Here's a brief explanation and example:

Using CustomBusinessDay with onOffset():

import pandas as pd # CustomBusinessDay with custom holidays holidays = ['2023-01-01', '2023-12-25'] custom_bday = pd.tseries.offsets.CustomBusinessDay(holidays=holidays) # Checking dates using onOffset date1 = pd.Timestamp('2023-01-01') # New Year's Day (a custom holiday) date2 = pd.Timestamp('2023-01-02') # Next day print(custom_bday.onOffset(date1)) # False, since it's a holiday print(custom_bday.onOffset(date2)) # True, since it's a business day 

In this example, we created a CustomBusinessDay frequency with custom holidays. We then used the onOffset() method to check if specific dates align with this custom business day frequency. The date '2023-01-01' returns False since it's one of the specified holidays, while the date '2023-01-02' returns True as it's a business day according to the rules we've defined.

Note: If you don't specify any holidays or custom weekmasks, CustomBusinessDay assumes the standard weekdays (Monday through Friday) as business days.


More Tags

idioms informix gdal tail higher-order-components android-toolbar rank switch-statement custom-fields

More Programming Guides

Other Guides

More Programming Examples