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
Selected Reading
Python Pandas - Return the rule code applied on the given BusinessDay object
The BusinessDay.rule_code property in Pandas returns the frequency rule code for a BusinessDay offset object. This property is useful for identifying the specific rule applied to business day calculations.
Syntax
BusinessDay.rule_code
Creating a BusinessDay Offset
First, let's create a BusinessDay offset object with a custom time offset ?
import datetime
import pandas as pd
# Create the BusinessDay Offset with time offset
bdOffset = pd.tseries.offsets.BusinessDay(offset=datetime.timedelta(hours=8, minutes=10))
print("BusinessDay Offset:", bdOffset)
print("Rule code:", bdOffset.rule_code)
BusinessDay Offset: <BusinessDay: offset=datetime.timedelta(seconds=29400)> Rule code: B
Complete Example
Here's a comprehensive example showing how to use the rule_code property with timestamps ?
import datetime
import pandas as pd
# Set the timestamp object
timestamp = pd.Timestamp('2021-10-30 01:55:02.000045')
print("Original Timestamp:", timestamp)
# Create the BusinessDay Offset
bdOffset = pd.tseries.offsets.BusinessDay(offset=datetime.timedelta(hours=8, minutes=10))
print("BusinessDay Offset:", bdOffset)
# Display the updated timestamp
updated_timestamp = timestamp + bdOffset
print("Updated Timestamp:", updated_timestamp)
# Get frequency string and rule code
print("Frequency string:", bdOffset.freqstr)
print("Rule code:", bdOffset.rule_code)
Original Timestamp: 2021-10-30 01:55:02.000045 BusinessDay Offset: <BusinessDay: offset=datetime.timedelta(seconds=29400)> Updated Timestamp: 2021-11-01 10:05:02.000045 Frequency string: B+8H10Min Rule code: B
Key Points
- The
rule_codeproperty returns'B'for BusinessDay offsets - It represents the base frequency rule without additional time offsets
- The
freqstrproperty shows the complete frequency string including time offsets - BusinessDay automatically skips weekends when calculating new dates
Conclusion
The rule_code property provides a simple way to identify the base frequency rule of a BusinessDay offset. It returns 'B' for business day frequencies, making it useful for programmatic frequency identification.
Advertisements
