Python Pandas -使用指定精度舍入時間增量


若要使用指定精度舍入時間增量,請使用 timestamp.round() 方法。使用 freq 引數設定精度。

首先,匯入必需的庫 -

import pandas as pd

建立一個時間增量物件

timedelta = pd.Timedelta('2 days 10 hours 45 min 20 s 35 ms 55 ns')

顯示時間增量

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

返回具有秒精度的舍入 Timestamp。此處,使用 "freq" 引數設定指定精度

timedelta.round(freq='s')

示例

以下是程式碼

import pandas as pd

# TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s
# create a Timedelta object
timedelta = pd.Timedelta('2 days 10 hours 45 min 20 s 35 ms 55 ns')

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

# return the rounded Timestamp
# with seconds frequency
# Here, the specified resolution is set using the "freq" parameter
res = timedelta.round(freq='s')

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

輸出

將生成以下程式碼

Timedelta...
2 days 10:45:20.035000055

Timedelta (seconds rounded)...
2 days 10:45:20

更新於: 14-Oct-2021

624 閱讀

開啟你的 職業生涯

完成本課程,獲得認證

開始吧
廣告