Python Pandas - 返回給定的 DateOffset 物件中的納秒數


若要返回給定 DateOffset 物件中的納秒數,請在 Pandas 中使用 offset.nanos 屬性。

首先,匯入必要的庫 −

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

在 Pandas 中設定時間戳物件 −

timestamp = pd.Timestamp('2021-08-30 03:08:02.000045')

建立一個 DateOffset。我們將使用“D”頻率增加天數 −

offset = to_offset("5D")

顯示更新後的時間戳 −

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

返回給定 DateOffset 物件中的納秒數 −

print("\nThe number of nanoseconds in the DateOffset object..\n", offset.nanos)

舉例

以下為程式碼 −

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

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-08-30 03:08: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 nanoseconds in the given DateOffset object
print("\nThe number of nanoseconds in the DateOffset object..\n", offset.nanos)

輸出

將生成以下程式碼 −

Timestamp...
 2021-08-30 03:08:02.000045

DateOffset...
 <5 * Days>

Updated Timestamp...
 2021-09-04 03:08:02.000045

The number of nanoseconds in the DateOffset object..
 432000000000000

更新於: 2021-10-21

81 次瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始使用
廣告
© . All rights reserved.