Python | Pandas tseries.offsets.CustomBusinessDay.rollforward

Python | Pandas tseries.offsets.CustomBusinessDay.rollforward

The CustomBusinessDay class from the pandas.tseries.offsets module provides a way to customize the weekdays that are considered business days. The rollforward method, which is available for many of the date offset objects in pandas, allows you to move a given date to the next "valid" date according to the rules of that offset.

For the CustomBusinessDay, the rollforward method will move a date to the next custom business day, considering any custom rules you've set up.

Here's an example of how you can use the CustomBusinessDay class along with the rollforward method:

import pandas as pd from pandas.tseries.offsets import CustomBusinessDay # Define a custom business day which excludes Wednesdays (i.e., 2) and Fridays (i.e., 4) weekmask = 'Mon Tue Thu Sat Sun' # Create an instance of CustomBusinessDay with the defined weekmask cbd = CustomBusinessDay(weekmask=weekmask) # Use rollforward to get the next custom business day date = pd.Timestamp('2023-09-20') # This is a Wednesday next_custom_business_day = cbd.rollforward(date) print(next_custom_business_day) # This will print '2023-09-21' which is a Thursday 

In the example above, since our custom business days exclude Wednesdays and Fridays, when we call rollforward on a Wednesday, it moves the date to the next valid business day, which is Thursday. If you had called rollforward on a Thursday, the result would have been the following Monday because Friday is not a custom business day in our example.

You can also customize holidays and other factors with CustomBusinessDay for even more specific date calculations.


More Tags

properties-file windows-store dynamic-jasper cdo-climate react-table-v6 mpmovieplayercontroller text-size connector katana kafka-python

More Programming Guides

Other Guides

More Programming Examples