Python Pandas - 返回在給定的 DateOffset 物件上應用的規則程式碼


若要返回在給定的 DateOffset 物件上應用的規則程式碼,請在 Pandas 中使用 offset.rule_code。首先,匯入所需的庫 −

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("3M")

顯示更新的時間戳 −

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

返回在給定的 DateOffset 物件上應用的頻率的規則程式碼 −

print("\nThe rule code of the DateOffset object..\n", offset.rule_code)

示例

以下為程式碼 −

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("3M")

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

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

# return the rule code of the frequency applied on the given DateOffset object
print("\nThe rule code of the DateOffset object..\n", offset.rule_code)

輸出

這將生成以下程式碼 −

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

DateOffset...
 <3 * MonthEnds>

Updated Timestamp...
 2021-11-30 03:25:02.000045

The rule code of the DateOffset object..
 M

更新於: 21-10-2021

84 瀏覽次數

開始您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.