Open In App

Python | Pandas tseries.offsets.CustomBusinessHour.rollback

Improve
Improve
Like Article
Like
Save
Share
Report

Dateoffsets are a standard kind of date increment used for a date range in Pandas. It works exactly like relativedelta in terms of the keyword args we pass in. DateOffsets work as follows, each offset specify a set of dates that conform to the DateOffset. For example, Bday defines this set to be the set of dates that are weekdays (M-F). DateOffsets can be created to move dates forward a given 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 on a valid date, first it is moved to a valid date and then offset is created. Pandas tseries.offsets.CustomBusinessHour.rollback() function is used to roll provided date backward to next offset only if not on offset.

Syntax: pandas.tseries.offsets.CustomBusinessHour.rollback(dt) Parameter : dt : date Returns : rollback

Example #1: Use pandas.tseries.offsets.CustomBusinessHour.rollback() function to roll the provided date backward if it is not on offset. 

Python3




# importing pandas as pd
import pandas as pd
 
# Creating Timestamp
ts = pd.Timestamp('2019-4-23 11:15:00')
 
# Create an offset
cbh = pd.tseries.offsets.CustomBusinessHour(n = 2, weekmask = 'Mon Tue Wed Thu',
                                           start ='12:00')
 
# Print the Timestamp
print(ts)
 
# Print the Offset
print(cbh)


Output : Now we will add the offset to the given timestamp object to increment the datetime value. We will also check if the given date is on offset or not. If not then we will roll backward the given date to the next offset. 

Python3




# Adding the offset to the given timestamp
new_timestamp = ts + cbh
 
# Print the updated timestamp
print(new_timestamp)
 
# roll backward if not on offset
result = cbh.rollback(pd.Timestamp('2019-4-28 11:15:00'))
 
# print the result
print(result)


Output : As we can see in the output, we have successfully created an offset and added it to the given timestamp. We have also rolled the date backward to next offset. Example #2: Use pandas.tseries.offsets.CustomBusinessHour.rollback() function to roll the provided date backward if it is not on offset. 

Python3




# importing pandas as pd
import pandas as pd
 
# Creating Timestamp
ts = pd.Timestamp('2019-4-23 11:15:00')
 
# Create an offset
cbh = pd.tseries.offsets.CustomBusinessHour(start ='07:00', end ='14:00')
 
# Print the Timestamp
print(ts)
 
# Print the Offset
print(cbh)


Output : Now we will add the offset to the given timestamp object to increment the datetime value. We will also check if the given date is on offset or not. If not then we will roll backward the given date to the next offset. 

Python3




# Adding the offset to the given timestamp
new_timestamp = ts + cbh
 
# Print the updated timestamp
print(new_timestamp)
 
# roll backward if not on offset
result = cbh.rollback(pd.Timestamp('2019-4-28 11:15:00'))
 
# print the result
print(result)


Output : As we can see in the output, we have successfully created an offset and added it to the given timestamp. We have also rolled the date backward to next offset.



Last Updated : 07 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads