Python Pandas - 建立一個 CustomBusinessHour 偏移量物件


若要建立 CustomBusinessHour 物件,請在 Pandas 中使用 pandas.tseries.offsets.CustomBusinessHour() 方法。

首先,匯入必需的庫 −

import pandas as pd

在 Pandas 中設定時間戳物件 −

timestamp = pd.Timestamp('2021-12-31 08:35:10')

建立 CustomBusinessHour 偏移量。CustomBusinessHour 是 DateOffset 子類。合法工作日的星期掩碼 −

cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 5, weekmask = 'Mon Tue Wed Fri')

顯示 CustomBusinessHour 偏移量 −

print("\nCustomBusinessHour Offset...\n",cbhOffset)

將偏移量新增到時間戳並顯示更新後的時間戳 −

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

範例

以下是程式碼 −

import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-12-31 08:35:10')

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

# Create the CustomBusinessHour Offset
# CustomBusinessHour is the DateOffset subclass
# Weekmask of valid business days
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 5, weekmask = 'Mon Tue Wed Fri')

# 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)

輸出

這將生成以下程式碼 −

Timestamp...
 2021-12-31 08:35:10

CustomBusinessHour Offset...
 <5 * CustomBusinessHours: CBH=09:00-17:00>

Updated Timestamp...
 2021-12-31 14:00:00

更新時間:2021 年 10 月 22 日

119 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告