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


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

首先,匯入所需的庫 −

import pandas as pd

在 Pandas 中設定時間戳物件 −

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

建立 CustomBusinessHour 偏移量。CustomBusinessHour 是 DateOffset 子類。我們使用“normalize”引數標準化了 CustomBusinessDay −

cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 3, weekmask = 'Mon Tue Wed Fri Sat' ,normalize=True)

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

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

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

print("\nThe CustomBusinessHour Offset is normalized ?\n", cbhOffset.normalize)

示例

以下是程式碼 −

import pandas as pd

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

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

# Create the CustomBusinessHour Offset
# CustomBusinessHour is the DateOffset subclass
# Weekmask of valid business days
# We have normalized the CustomBusinessDay using the "normalize" parameter
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 3, weekmask = 'Mon Tue Wed Fri Sat',normalize=True)

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

# Return frequency applied on the given CustomBusinessHour Offset object as a string
print("\nFrequency applied on the given CustomBusinessHour Offset object...\n",cbhOffset.freqstr)

# return the name of the frequency applied on the given CustomBusinessHour object
print("\nThe name of the frequency on the CustomBusinessHour object..\n", cbhOffset.name)

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

輸出

這將生成以下程式碼 −

Timestamp...
 2021-10-25 08:35:10

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

Updated Timestamp...
 2021-10-25 00:00:00

Frequency applied on the given CustomBusinessHour Offset object...
 3CBH

The name of the frequency on the CustomBusinessHour object..
 CBH

The CustomBusinessHour Offset is normalized ?
 True

更新於: 22-10-2021

78 次瀏覽

開啟你的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.