Python Pandas - 建立 DateOffset 並增量日期


使用 Pandas 中的 DateOffset() 函式來建立 DateOffset。將增量值設定為一個引數。

首先,匯入所需的庫 −

from pandas.tseries.offsets import DateOffset
import pandas as pd

在 Pandas 中設定時間戳物件 −

timestamp = pd.Timestamp('2021-09-11 02:30:55')

日期增量的 DateOffset。我們這裡使用 "months" 引數來增量月份 −

print("DateOffset...\n",timestamp + DateOffset(months=2))

示例

以下為程式碼 −

from pandas.tseries.offsets import DateOffset
import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-09-11 02:30:55')

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

# DateOffset for date increment
# We are incrementing the months here using the "months" parameter
print("DateOffset...\n",timestamp + DateOffset(months=2))

輸出

將產生以下程式碼 −

Timestamp...
2021-09-11 02:30:55
DateOffset...
2021-11-11 02:30:55

更新於: 21-10-2021

1 千次觀看

開啟 職業生涯

透過完成課程獲得認證

開始
廣告