Python Pandas - Create a BusinessDay offset


To create a BusinessDay offset, use the pd.tseries.offsets.BusinessDay() method in Pandas. At first, import the required libraries −

import datetime
import pandas as pd

Create the BusinessDay Offset. BusinessDay is the DateOffset subclass −

bdOffset = pd.tseries.offsets.BusinessDay(offset = datetime.timedelta(days = 7, hours = 7, minutes =
7))

Display the BusinessDay Offset −

print("BusinessDay Offset...\n",bdOffset)

Set the timestamp object in Pandas −

timestamp = pd.Timestamp('2021-1-1 01:55:02.000045')

Display the Updated Timestamp −

print("\nUpdated Timestamp...\n",timestamp + bdOffset)

Example

Following is the code −

import datetime
import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-1-1 01:55:02.000045')

# Display the Timestamp
print("Timestamp...\n",timestamp)

# Create the BusinessDay Offset
# BusinessDay is the DateOffset subclass
bdOffset = pd.tseries.offsets.BusinessDay(offset = datetime.timedelta(days = 7, hours = 7, minutes = 7))

# Display the BusinessDay Offset
print("BusinessDay Offset...\n",bdOffset)

# Display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + bdOffset)

Output

This will produce the following code −

Timestamp...
 2021-01-01 01:55:02.000045
BusinessDay Offset...
 <BusinessDay: offset=datetime.timedelta(days=7, seconds=25620)>

Updated Timestamp...
 2021-01-11 09:02:02.000045

Updated on: 21-Oct-2021

155 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements