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 - Display the start time of the custom business hour in 24h format from the BusinessHour offset object
To display the start time of the custom business hour in 24h format from the BusinessHour offset object, use the BusinessHour.start property. This property returns a tuple containing the start time as a datetime.time object.
Creating a BusinessHour Offset
First, let's create a BusinessHour offset with custom start and end times ?
import pandas as pd
# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-9-30 06:50:20')
# Display the Timestamp
print("Timestamp...\n", timestamp)
# Create the BusinessHour Offset
# "start" is the start time of your custom business hour in 24h format
# "end" is the end time of your custom business hour in 24h format
bhOffset = pd.tseries.offsets.BusinessHour(start="09:30", end="18:00", n=8)
# Display the BusinessHour Offset
print("\nBusinessHour Offset...\n", bhOffset)
Timestamp... 2021-09-30 06:50:20 BusinessHour Offset... <8 * BusinessHours: BH=09:30-18:00>
Accessing the Start Time
Use the start property to retrieve the start time of the custom business hour ?
import pandas as pd
# Create the BusinessHour Offset
bhOffset = pd.tseries.offsets.BusinessHour(start="09:30", end="18:00", n=8)
# Display the start time of the custom business hour
print("The start time of the custom business hour...\n", bhOffset.start)
# You can also access individual time components
start_time = bhOffset.start[0] # First element of the tuple
print("\nStart time as datetime.time object:", start_time)
print("Hour:", start_time.hour)
print("Minute:", start_time.minute)
The start time of the custom business hour... (datetime.time(9, 30),) Start time as datetime.time object: 09:30:00 Hour: 9 Minute: 30
Complete Example
Here's a complete example showing how to work with BusinessHour offset and display the start time ?
import pandas as pd
# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-9-30 06:50:20')
# Create the BusinessHour Offset
bhOffset = pd.tseries.offsets.BusinessHour(start="09:30", end="18:00", n=8)
# Display the Updated Timestamp
print("Updated Timestamp...\n", timestamp + bhOffset)
# Display the start time of the custom business hour
print("\nThe start time of the custom business hour...\n", bhOffset.start)
# Format the start time for better readability
start_time = bhOffset.start[0]
print(f"\nFormatted start time: {start_time.strftime('%H:%M')}")
Updated Timestamp... 2021-09-30 17:30:00 The start time of the custom business hour... (datetime.time(9, 30),) Formatted start time: 09:30
Conclusion
The BusinessHour.start property returns the start time as a tuple containing datetime.time objects. Access the first element to work with the time object and use strftime() for custom formatting.
Advertisements
