Python Pandas - 檢查 BusinessHour 偏移量是否已標準化


如需檢查 BusinessHour 偏移量是否已標準化,請在 Pandas 中使用 BusinessHour.normalize 屬性。

首先,匯入所需的庫 −

import pandas as pd

在 Pandas 中設定時間戳物件 −

timestamp = pd.Timestamp('2021-1-1 01:55:30')

建立 BusinessHour 偏移量。我們使用“normalize”引數標準化了 BusinessHour −

bhOffset = pd.tseries.offsets.BusinessHour(start="09:30", end = "18:00", normalize=True)

顯示更新的時間戳 −

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

檢查 BusinessHour 偏移量是否已標準化 −

print("\nThe BusinessHour Offset is normalized ?\n", bhOffset.normalize)

示例

以下是程式碼 −

import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-1-1 01:55:30')

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

# Create the BusinessHour Offset
# BusinessHour 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.
# We have normalized the BusinessHour using the "normalize" parameter
bhOffset = pd.tseries.offsets.BusinessHour(start="09:30", end = "18:00", normalize=True)

# Display the BusinessHour Offset
print("\nBusinessHour Offset...\n",bhOffset)

# Display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + bhOffset)

# check whether the BusinessHour Offset is normalized or not
print("\nThe BusinessHour Offset is normalized ?\n", bhOffset.normalize)

輸出

這會產生以下程式碼 −

Timestamp...
 2021-01-01 01:55:30

BusinessHour Offset...
 <BusinessHour: BH=09:30-18:00>

Updated Timestamp...
 2021-01-01 00:00:00

The BusinessHour Offset is normalized ?
 True

更新於: 2021-10-21

80 次瀏覽

開啟你的職業生涯

透過課程獲取認證

開始學習
廣告
© . All rights reserved.