Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Pandas BusinessHour offset object - Move to the next business day
The BusinessHour offset in Pandas provides the next_bday property to move timestamps to the next business day. This is useful for financial calculations and business logic where weekends need to be skipped.
Understanding BusinessHour Offset
The BusinessHour is a DateOffset subclass that handles business day calculations. It automatically skips weekends and can include custom time offsets ?
import datetime
import pandas as pd
# Create a BusinessHour offset with custom offset
bhOffset = pd.tseries.offsets.BusinessHour(offset=datetime.timedelta(days=3, hours=3))
print("BusinessHour Offset:", bhOffset)
BusinessHour Offset: <BusinessHour: offset=datetime.timedelta(days=3, seconds=10800): BH=09:00-17:00>
Moving to Next Business Day
Use the next_bday property to move any timestamp to the next business day while preserving the time component ?
import datetime
import pandas as pd
# Set the timestamp object (Thursday, September 30, 2021)
timestamp = pd.Timestamp('2021-9-30 06:50:20')
print("Original Timestamp:", timestamp)
print("Day of week:", timestamp.strftime('%A'))
# Create BusinessHour Offset
bhOffset = pd.tseries.offsets.BusinessHour(offset=datetime.timedelta(days=3, hours=3))
# Move to next business day
next_bday = timestamp + bhOffset.next_bday
print("Next business day:", next_bday)
print("Day of week:", next_bday.strftime('%A'))
Original Timestamp: 2021-09-30 06:50:20 Day of week: Thursday Next business day: 2021-10-01 06:50:20 Day of week: Friday
Weekend Handling Example
When the current date is a Friday, next_bday correctly skips the weekend ?
import datetime
import pandas as pd
# Friday timestamp
friday_timestamp = pd.Timestamp('2021-10-01 14:30:00')
print("Friday Timestamp:", friday_timestamp)
print("Day of week:", friday_timestamp.strftime('%A'))
# Create BusinessHour Offset
bhOffset = pd.tseries.offsets.BusinessHour()
# Move to next business day (should be Monday)
next_bday = friday_timestamp + bhOffset.next_bday
print("Next business day:", next_bday)
print("Day of week:", next_bday.strftime('%A'))
Friday Timestamp: 2021-10-01 14:30:00 Day of week: Friday Next business day: 2021-10-04 14:30:00 Day of week: Monday
Key Properties
| Property | Description | Example Use |
|---|---|---|
next_bday |
Moves to next business day | Settlement calculations |
| Time preservation | Keeps original time component | Scheduling systems |
| Weekend skipping | Automatically handles weekends | Financial workflows |
Conclusion
The next_bday property of BusinessHour offset efficiently moves timestamps to the next business day while preserving time components. It automatically handles weekend skipping, making it ideal for business and financial applications.
