Python Pandas - 將給定的BusinessDay Offset物件頻率作為字串返回


要將給定的BusinessDay Offset物件的應用頻率作為字串返回,請在Pandas中使用BusinessDay.freqstr屬性。

首先,匯入所需的庫:

import datetime
import pandas as pd

在Pandas中設定時間戳物件:

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

建立BusinessDay Offset。BusinessDay是DateOffset的子類:

bdOffset = pd.tseries.offsets.BusinessDay(offset = datetime.timedelta(days = 7, hours = 7, minutes = 7))

顯示更新後的時間戳:

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

將應用於給定BusinessDay物件的頻率作為字串返回:

print("\nFrequency on the given BusinessDay Offset...\n",bdOffset.freqstr)

示例

以下是程式碼:

import datetime
import pandas as pd

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

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

# Create the BusinessDay Offset
# BusinessDay is the DateOffset subclass
bdOffset = pd.tseries.offsets.BusinessDay(offset = datetime.timedelta(days = 7, hours = 7, minutes = 7))

# Display the BusinessDay Offset
print("BusinessDay 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)

輸出

這將產生以下程式碼:

Timestamp...
 2021-01-01 01:55:02.000045
BusinessDay Offset...
 <BusinessDay: offset=datetime.timedelta(days=7, seconds=25620)>

Updated Timestamp...
 2021-01-11 09:02:02.000045

Frequency on the given BusinessDay Offset...
 B+7D7H7Min

更新於:2021年10月21日

67 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告