Python Pandas BusinessHour 偏移物件 - 移至下一個工作日


使用 Pandas 中的 BusinessHour.next_bday 屬性將當前時間移動到下一個工作日。首先匯入所需的庫 -

import datetime
import pandas as pd

建立 BusinessHour 偏移。BusinessHour 是 DateOffset 子類 -

bhOffset = pd.tseries.offsets.BusinessHour(offset = datetime.timedelta(days = 3, hours = 3))

顯示 BusinessHour 偏移 -

print("\nBusinessHour Offset...\n",bhOffset)

在 Pandas 中設定時間戳物件 -

timestamp = pd.Timestamp('2021-9-30 06:50:20')

顯示下一個工作日 -

print("\nThe next business day...\n",timestamp + bhOffset.next_bday)

示例

以下是程式碼 -

import datetime
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
# BusinessHour is the DateOffset subclass
bhOffset = pd.tseries.offsets.BusinessHour(offset = datetime.timedelta(days = 3, hours = 3))
# Display the BusinessHour Offset
print("\nBusinessHour Offset...\n",bhOffset)

# Display the next business day
print("\nThe next business day...\n",timestamp + bhOffset.next_bday)

輸出

此程式碼會產生以下 -

Timestamp...
 2021-09-30 06:50:20

BusinessHour Offset...
 <BusinessHour: offset=datetime.timedelta(days=3, seconds=10800): BH=09:00-17:00>

The next business day...
 2021-10-01 06:50:20

更新於: 2021-10-21

239 次瀏覽

啟動你的 職業生涯

透過完成課程獲取認證

開始
廣告