Python Pandas——返回對所給 DateOffset 物件應用的增量計數


要返回對給定 DateOffset 物件應用的增量計數,請在 Pandas 中使用 offset.n 屬性。首先,匯入所需的庫 −

from pandas.tseries.frequencies import to_offset
import pandas as pd

在 Pandas 中設定時間戳物件 −

timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')

建立 DateOffset。我們在此處使用“M”頻率來增加月數 −

offset = to_offset("5M")

顯示更新的時間戳 −

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

返回對給定 DateOffset 物件的增量計數 −

print("\nThe count of increments on the DateOffset object..\n", offset.n)

例項

以下是程式碼 −

from pandas.tseries.frequencies import to_offset
import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')

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

# Create the DateOffset
# We are incrementing the months here using the "M" frequency
offset = to_offset("5M")

# Display the DateOffset
print("\nDateOffset...\n",offset)

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

# return the count of increments on the given DateOffset object
print("\nThe count of increments on the DateOffset object..\n", offset.n)

輸出

將生成以下程式碼 −

Timestamp...
 2021-09-26 03:25:02.000045

DateOffset...
 <5 * MonthEnds>

Updated Timestamp...
 2022-01-31 03:25:02.000045

The count of increments on the DateOffset object..
 5

更新日期:2021-10-21

63 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始吧
廣告
© . All rights reserved.