Change language

Python | Pandas tseries.offsets.BusinessHour

| |

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.BusinessHour() Pandas tseries.offsets.BusinessHour () is used to create a business hour offset.

Syntax: pandas.tseries. offsets.BusinessHour (n = 1, normalize = False, start = ’09: 00 ′, end = ’17: 00 ′, offset = datetime.timedelta (0))

Parameter:
n: number of hours
normalize: Whether to round the result of a DateOffset addition down to the previous midnight.
start: start time
end: end time
offset: offset from the given date

Returns: offset

Example # 1: Use pandas.tseries.offsets.BusinessHour () to create an offset in 5 pandas.tse ries.offsets.BusinessHour () hours.

# import pandas as pd

import pandas as pd

 
# Create a timestamp

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

 
# Create offset 5 business hours

bh = pd.tseries.offsets.BusinessHour (n = 5 )

 
# Print from timestamps

print (ts)

 
# Print offset

print (bh)

Output:

We will now add a business hour offset to this timestamp object to increase the datetime.

# Add a business hour offset to the specified timestamp

new_timestamp = ts + bh

 

 
# Print updated timestamp

print (new_timestamp)

Output:

As we can see from the output, we have successfully created an offset of 5 business hours and added it to the given timestamp.

Example # 2: Use pandas.tseries.offsets.BusinessHour () to create an offset of 10 days and 5 pandas.tseries.offsets.BusinessHour () hours.

# import pandas as pd

import pandas as pd

 
# building a timestamp

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

 
# Create offset

bh = pd.tseries.offsets.BusinessHour (offset = datetime.timedelta (days = 10 , hours = 10 ))

 
# Print time stamp

print (ts)

 
# Print offset

print (bh)

Output:

Now we will add the work hour offset to given a timestamp object to increment the datetime value.

# Add a business hour offset to the specified time stamp

new_timestamp = ts + bh

 
# Print updated timestamp

print (new_timestamp)  

Output:

As we can see in the output, we have successfully created an offset and added it to the given timestamp.