Python Pandas - 返回應用於給定 DateOffset 物件的頻率


若要返回應用於給定 DateOffset 物件的頻率,請使用 Pandas 中的 offset.freqstr。首先,匯入所需的庫 −

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

在 Pandas 中設定時間戳物件 −

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

建立 DateOffset。這裡使用“D”頻率遞增天數 −

offset = to_offset("5D")

顯示更新的時間戳 −

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

返回應用於給定 DateOffset 物件的頻率 −

print("\nThe frequency on the DateOffset object..\n", offset.freqstr)

示例

程式碼如下 −

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 days here using the "D" frequency
offset = to_offset("5D")

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

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

# return the frequency applied on the given DateOffset object
print("\nThe frequency on the DateOffset object..\n", offset.freqstr)

輸出

這將產生以下程式碼 −

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

DateOffset...
 <5 * Days>

Updated Timestamp...
 2021-10-01 03:25:02.000045

The frequency on the DateOffset object..
 5D

更新於: 2021-10-21

84 瀏覽次數

開啟你的 職業生涯

透過完成本課程獲得認證

開始學習
廣告
© . All rights reserved.