Python Pandas - 檢查 BusinessDay 偏移是否已被規範化


若要檢查 BusinessDay 偏移是否已被規範化,請在 Pandas 中使用 BusinessDay.normalize 屬性。

首先,匯入必需的庫 −

import datetime
import pandas as pd

在 Pandas 中設定時間戳物件 −

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

建立 BusinessDay 偏移。BusinessDay 是 DateOffset 子類。我們使用“normalize”引數對 BusinessDay 進行了規範化 −

bdOffset = pd.tseries.offsets.BusinessDay(offset = datetime.timedelta(hours = 8, minutes = 10), normalize=True)

顯示更新的時間戳 −

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

檢查 BusinessDay 偏移是否已規範化 −

print("\nThe BusinessDay Offset is normalized..\n", bdOffset.normalize)

示例

以下為程式碼 −

import datetime
import pandas as pd

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

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

# Create the BusinessDay Offset
# BusinessDay is the DateOffset subclass
# We have normalized the BusinessDay using the "normalize" parameter
bdOffset = pd.tseries.offsets.BusinessDay(offset = datetime.timedelta(hours = 8, minutes = 10), normalize=True)

# Display the BusinessDay Offset
print("\nBusinessDay Offset...\n",bdOffset)

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

# return the frequency applied on the given BusinessDay object as a string
print("\nFrequency on the given BusinessDay Offset...\n",bdOffset.freqstr)

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

# check whether the BusinessDay Offset is normalized or not
print("\nThe BusinessDay Offset is normalized..\n", bdOffset.normalize)

輸出

這將產生以下程式碼 −

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

BusinessDay Offset...
 <BusinessDay: offset=datetime.timedelta(seconds=29400)>

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

Frequency on the given BusinessDay Offset...
 B+8H10Min

The name of the frequency on the BusinessDay object..
 B

The BusinessDay Offset is normalized..
 True

更新日期: 21-10-2021

101 次瀏覽

開啟您的 職業

完成課程並獲得認證

立即開始
廣告