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 - Check if the given CustomBusinessHour is Anchored
To check if a given CustomBusinessHour is anchored, use the is_anchored() method in Pandas. An anchored offset is tied to a specific point in time, like the beginning of a month or day.
What is CustomBusinessHour?
CustomBusinessHour is a DateOffset subclass that allows you to define custom business hours with specific start and end times ?
import pandas as pd
# Create a CustomBusinessHour offset with business hours 9:30 AM to 6:30 PM
cbh_offset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end="18:30")
print("CustomBusinessHour Offset:", cbh_offset)
CustomBusinessHour Offset: <CustomBusinessHour: CBH=09:30-18:30>
Checking if CustomBusinessHour is Anchored
The is_anchored() method returns True if the offset is anchored, False otherwise ?
import pandas as pd
# Create CustomBusinessHour offset
cbh_offset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end="18:30")
# Check if it's anchored
is_anchored = cbh_offset.is_anchored()
print("Is CustomBusinessHour anchored?", is_anchored)
Is CustomBusinessHour anchored? True
Complete Example with Timestamp Addition
Here's how CustomBusinessHour works with timestamps and anchoring ?
import pandas as pd
# Set the timestamp object
timestamp = pd.Timestamp('2021-11-14 05:20:30')
print("Original Timestamp:", timestamp)
# Create CustomBusinessHour offset (9:30 AM to 6:30 PM)
cbh_offset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end="18:30")
print("CustomBusinessHour Offset:", cbh_offset)
# Add the offset to the timestamp
updated_timestamp = timestamp + cbh_offset
print("Updated Timestamp:", updated_timestamp)
# Check if the offset is anchored
print("Is anchored?", cbh_offset.is_anchored())
Original Timestamp: 2021-11-14 05:20:30 CustomBusinessHour Offset: <CustomBusinessHour: CBH=09:30-18:30> Updated Timestamp: 2021-11-15 10:30:00 Is anchored? True
Understanding the Result
CustomBusinessHour returns True for is_anchored() because it's tied to specific business hour boundaries. When you add it to a timestamp, it moves to the next available business hour within the defined range.
Conclusion
The is_anchored() method helps identify if a DateOffset is tied to specific time boundaries. CustomBusinessHour is always anchored because it operates within defined business hour ranges.
