Python Pandas - 使用字串輸入從 Timedelta 物件返回微秒


要從 Timedelta 物件返回納秒,請使用 timedelta.microseconds 屬性。首先,匯入必需的庫 −

import pandas as pd

TimeDeltas 是 Python 的標準 datetime 庫使用 Unit“us”中用於微秒的不同的表示 timedelta。設定字串輸入為微秒。建立一個 Timedelta 物件

timedelta = pd.Timedelta('12 min 40 us')

顯示 Timedelta

print("Timedelta...\n", timedelta)

返回微秒值

timedelta.microseconds

示例

以下為程式碼

import pandas as pd

# TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s
# set string input for microseconds using unit 'us'
# create a Timedelta object
timedelta = pd.Timedelta('12 min 40 us')

# Display the Timedelta:
print("Timedelta...\n", timedelta)

# return the microseconds value
res = timedelta.microseconds

# display the microseconds
print("\nTimedelta (microseconds value)...\n", res)

輸出

將生成以下程式碼

Timedelta...
0 days 00:12:00.040000

Timedelta (microseconds value)...
40000

已更新為:13-Oct-2021

166 次瀏覽

開啟你的 職業生涯

完成課程並獲得認證

立即開始
廣告