Python Pandas - 檢查給定的 CustomBusinessHour 是否已固定


要檢查是否已經固定給定的 CustomBusinessHour,請在 Pandas 中使用 CustomBusinessHour.is_anchored() 方法。

首先,匯入所需的庫 -

import pandas as pd

在 Pandas 中設定時間戳物件 -

timestamp = pd.Timestamp('2021-11-14 05:20:30')

建立 CustomBusinessHour 偏移量。CustomBusinessHour 是 DateOffset 子類 -

cbhOffset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end = "18:30")

向時間戳新增偏移量並顯示更新的時間戳 -

print("\nUpdated Timestamp...\n",timestamp + cbhOffset)

檢查是否已固定 CustomBusinessHour -

print("\nCheck whether the CustomBusinessHour is anchored...\n", cbhOffset.is_anchored())

示例

以下是程式碼 -

import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-11-14 05:20:30')

# Display the Timestamp
print("Timestamp...\n",timestamp)

# Create the CustomBusinessHour Offset
# CustomBusinessHour is the DateOffset subclass
# Here, "start" is the start time of your custom business hour in 24h format.
# The "end" is the end time of your custom business hour in 24h format.
cbhOffset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end = "18:30")

# Display the CustomBusinessHour Offset
print("\nCustomBusinessHour Offset...\n",cbhOffset)

# Add the offset to the Timestamp and display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + cbhOffset)

# Check whether the CustomBusinessHour is anchored
print("\nCheck whether the CustomBusinessHour is anchored...\n", cbhOffset.is_anchored())

輸出

它會生成以下程式碼 -

Timestamp...
 2021-11-14 05:20:30

CustomBusinessHour Offset...
 <CustomBusinessHour: CBH=09:30-18:30>

Updated Timestamp...
 2021-11-15 10:30:00

Check whether the CustomBusinessHour is anchored...
 True

更新於:2021 年 10 月 22 日

71 次瀏覽

啟動你的 事業

完成課程即可獲得認證

入門
廣告
© . All rights reserved.