Python Pandas - 從時間增量物件返回秒


要從時間增量物件返回秒,請使用 timedelta.seconds 屬性。首先,匯入必需的庫 −

import pandas as pd

Python 標準 datetime 庫使用不同的表示法 “timedelta”。建立一個時間增量物件

timedelta = pd.Timedelta('10 s 15 ms 33 ns')

顯示時間增量

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

返回秒值

timedelta.seconds

示例

下面的程式碼

import pandas as pd

# TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s
# create a Timedelta object
timedelta = pd.Timedelta('10 s 15 ms 33 ns')

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

# return the seconds value
res = timedelta.seconds

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

輸出

將生成以下程式碼

Timedelta...
0 days 00:00:10.015000033

Timedelta (seconds value)...
10

更新日期:2021-10-13

檢視 193 次

開拓您的職業生涯

完成課程獲得認證

開始學習
廣告