Python Pandas - 返回對業務日偏移應用的增加計數


要返回對 BusinessDay 偏移應用的增加計數,請在 Pandas 中使用 BusinessDay.n 屬性。

首先,匯入所需的庫 −

import datetime
import pandas as pd

在 Pandas 中設定時間戳物件” −

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

建立 BusinessDay 偏移。BusinessDay 是 DateOffset 子類 −

bdOffset = pd.tseries.offsets.BusinessDay(n = 6)

顯示更新後的時間戳 −

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

返回給定 BusinessDay 物件上的增量計數 −

print("\nThe count of increments on the BusinessDay object..\n", bdOffset.n)

示例

以下是程式碼 −

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
bdOffset = pd.tseries.offsets.BusinessDay(n = 6)

# 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 count of increments on the given BusinessDay object
print("\nThe count of increments on the BusinessDay object..\n", bdOffset.n)

輸出

這將生成以下程式碼 −

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

BusinessDay Offset...
 <6 * BusinessDays>

Updated Timestamp...
 2021-11-08 01:55:02.000045

Frequency on the given BusinessDay Offset...
 6B

The count of increments on the BusinessDay object..
 6

更新於:21-Oct-2021

82 次瀏覽

啟動您的 事業

透過完成本課程獲得認證

開始學習
廣告