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 - Get the weekmask applied on the CustomBusinessDay offset
The weekmask property in Pandas allows you to retrieve the custom business days pattern applied to a CustomBusinessDay offset. This is useful for understanding which days of the week are considered valid business days in your custom offset.
What is CustomBusinessDay?
CustomBusinessDay is a DateOffset subclass that represents custom business days while excluding holidays. Unlike standard business days (Mon-Fri), you can specify your own pattern of valid business days using a weekmask.
Creating a CustomBusinessDay with Weekmask
First, let's create a timestamp and a CustomBusinessDay offset with a custom weekmask ?
import pandas as pd
# Set the timestamp object
timestamp = pd.Timestamp('2021-10-22 03:10:35')
print("Original Timestamp:", timestamp)
# Create CustomBusinessDay with custom weekmask (Mon, Tue, Wed, Fri only)
cbdOffset = pd.tseries.offsets.CustomBusinessDay(n=4, weekmask='Mon Tue Wed Fri')
print("CustomBusinessDay Offset:", cbdOffset)
Original Timestamp: 2021-10-22 03:10:35 CustomBusinessDay Offset: <4 * CustomBusinessDays>
Getting the Weekmask Property
Use the weekmask property to retrieve the business days pattern applied to the offset ?
import pandas as pd
timestamp = pd.Timestamp('2021-10-22 03:10:35')
cbdOffset = pd.tseries.offsets.CustomBusinessDay(n=4, weekmask='Mon Tue Wed Fri')
# Get the weekmask applied
print("Weekmask:", cbdOffset.weekmask)
# Add offset to timestamp
updated_timestamp = timestamp + cbdOffset
print("Updated Timestamp:", updated_timestamp)
# Other useful properties
print("Frequency string:", cbdOffset.freqstr)
print("Number of increments:", cbdOffset.n)
Weekmask: Mon Tue Wed Fri Updated Timestamp: 2021-10-29 03:10:35 Frequency string: 4C Number of increments: 4
Complete Example
Here's a comprehensive example showing how to work with CustomBusinessDay and retrieve its weekmask ?
import pandas as pd
# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-10-22 03:10:35')
# Display the Timestamp
print("Timestamp...\n", timestamp)
# Create the CustomBusinessDay Offset
# CustomBusinessDay is the DateOffset subclass representing custom business days excluding holidays
# Weekmask of valid business days
cbdOffset = pd.tseries.offsets.CustomBusinessDay(n=4, weekmask='Mon Tue Wed Fri')
# Display the CustomBusinessDay Offset
print("\nCustomBusinessDay Offset...\n", cbdOffset)
# Add the offset to the Timestamp and display the Updated Timestamp
print("\nUpdated Timestamp...\n", timestamp + cbdOffset)
# Return frequency applied on the given CustomBusinessDay Offset object as a string
print("\nFrequency applied on the given CustomBusinessDay Offset object...\n", cbdOffset.freqstr)
# return the count of increments on the given CustomBusinessDay object
print("\nThe count of increments on the CustomBusinessDay object..\n", cbdOffset.n)
# display the weekmask
print("\nThe weekmask on the CustomBusinessDay object..\n", cbdOffset.weekmask)
Timestamp... 2021-10-22 03:10:35 CustomBusinessDay Offset... <4 * CustomBusinessDays> Updated Timestamp... 2021-10-29 03:10:35 Frequency applied on the given CustomBusinessDay Offset object... 4C The count of increments on the CustomBusinessDay object.. 4 The weekmask on the CustomBusinessDay object.. Mon Tue Wed Fri
Key Properties
| Property | Description | Example Output |
|---|---|---|
weekmask |
Returns the custom business days pattern | Mon Tue Wed Fri |
freqstr |
Returns frequency as string | 4C |
n |
Returns number of increments | 4 |
Conclusion
Use the weekmask property to retrieve the custom business days pattern from a CustomBusinessDay offset. This property returns a string showing which days are considered valid business days in your custom offset configuration.
