Python Pandas - 返回應用於偏移量物件的頻率名稱


要返回應用於偏移量物件的頻率名稱,請在 Pandas 中使用 offset.name 屬性。首先,匯入所需的庫 -

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 name of the frequency on the DateOffset object..\n",
offset.name)

示例

以下是程式碼 -

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 name of the frequency applied on the given DateOffset object
print("\nThe name of the frequency on the DateOffset object..\n", offset.name)

輸出

這將生成以下程式碼 -

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

DateOffset...
 <3 * MonthEnds>

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

The name of the frequency on the DateOffset object..
 M

更新於: 2021年10月21日

81 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.