Change language

Python | Pandas tseries.offsets.BusinessDay.rollforward

| |

DateOffsets can be created to move dates by a specified number of valid dates. For example, Bday (2) can be added to a date to move it two business days forward. If the date does not start with a valid date, it is first moved to a valid date and then an offset is generated.

tseries.offsets.BusinessDay.rollforward() Pandas tseries.offsets.BusinessDay.rollforward() provided date forward until next offset, unless offset.

Syntax : pandas.tseries.offsets.BusinessDay.rollforward (dt)

Parameter:
dt: dt

Returns: Timestamp

Example # 1: Use pandas.tseries.offsets.BusinessDay.rollforward ( ) to scroll the supplied date forward to the next offset, if not at an offset.

# import pandas as pd

import pandas as pd

 
# Create a timestamp

ts = pd.Timestamp ( ’ 2019-10-10 07: 15: 11’ )

  
# Create offset 5 business days

bd = pd.tseries.offsets.BusinessDay (n = 5 )

 
# Print timestamps

print (ts)

 
# Print DateOffset

print (bd)

Output:

We will now add the workday offset to this timestamp object to increase the datetime value. We’ll also roll the provided date forward to the next offset, if not at the offset.

# Add a workday offset to this timestamp

new_timestamp = ts + bd

 
# Print updated timestamp

print (new_timestamp)

 
# roll forward the provided date if not
# at offset

date = bd. rollforward (dt = pd.to_datetime ( ’2010-02-13’ ))

 
# print date

print (date)

Output:

As we can see from the output, we successfully created an offset of 5 business days and added it to the specified timestamp. We’ve also rolled the provided date forward to the next offset, if not at an offset.

Example # 2: Use pandas.tseries.offsets.BusinessDay.rollforward () to pandas.tseries.offsets.BusinessDay.rollforward () the date supplied forward to the next offset, if not in offset.

# import pandas as pd

import pandas as pd

 
# Create a timestamp

ts = pd.Timestamp ( ’2019-10-10 07: 15: 11’ )

 
# Create an offset of 10 working days and 10 hours

bd = pd.tseries.offsets.BusinessDay (offset = datetime.timedelta (days = 10 , hours = 10 ))

 
# Print timestamps

print (ts)

 
# Print DateOffset

print (bd)

Output:

 

We will now add the workday offset to this timestamp object to increment the datetime value. We’ll also roll the provided date forward to the next offset, if not at the offset.

# Add a workday offset to this timestamp

new_timestamp = ts + bd

 
# Print updated timestamp

print (new_timestamp)

 
# roll forward the provided date if not
# at offset

date = bd. rollforward (dt = pd.to_datetime ( ’2010-02-13’ ))

 
# print date

print (date)

Output:

As we can see from the output, we have successfully created an offset of 10 business days and 10 hours and added it to the given mark time. We’ve also rolled the provided date forward to the next offset, if not at the offset.