Python Pandas - 檢查 DateOffset 值是否已標準化


若要檢查 DateOffset 值是否已標準化,請在 Pandas 中使用 offset.normalize 屬性。

首先,匯入所需的庫 −

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

在 Pandas 中設定時間戳物件 −

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

建立 DateOffset。在此使用 “months” 引數遞增月份。我們使用 “normalize” 引數對 DateOffset 進行了標準化 −

offset = pd.tseries.offsets.DateOffset(months=4, normalize=True)

顯示已更新的時間戳 −

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

檢查 DateOffset 是否已標準化 −

print("\nThe DateOffset is normalized..\n", offset.normalize)

示例

以下是程式碼 −

from pandas.tseries.offsets import DateOffset
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
# Incrementing the months here using the "months" parameter
# We have normalized the DateOffset using the "normalize" parameter
offset = pd.tseries.offsets.DateOffset(months=4, normalize=True)

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

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

# check whether the DateOffset is normalized or not
print("\nThe DateOffset is normalized..\n", offset.normalize)

輸出

這將生成以下程式碼 −

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

DateOffset...
 <DateOffset: months=4>

Updated Timestamp...
 2022-01-26 00:00:00

The DateOffset is normalized..
 True

更新於: 2021-10-21

142 次瀏覽

開啟你的事業

完成課程認證

開始
廣告
© . All rights reserved.