Python Pandas - 按分鐘頻率舍入 Timedelta
要按指定解析度舍入 Timedelta,請使用 timestamp.round() 方法。使用 freq 引數設定分鐘頻率解析度,值為 T。
首先,匯入必需的庫 −
import pandas as pd
TimeDeltas 是 Python 的標準 datetime 庫,使用一個 timedelta 的不同表示法。建立一個 Timedelta 物件 −
timedelta = pd.Timedelta('2 days 10 hours 45 min 20 s 35 ms 55 ns')
顯示 Timedelta −
print("Timedelta...\n", timedelta)
返回舍入後具有分鐘頻率的時間戳。此處,使用“freq”引數設定指定的解析度 −
timedelta.round(freq='T')
例項
以下為程式碼 −
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 minutely frequency # Here, the specified resolution is set using the "freq" parameter res = timedelta.round(freq='T') # display the rounded Timestamp print("\nTimedelta (minutely rounded)...\n", res)
輸出
將產生以下程式碼 −
Timedelta... 2 days 10:45:20.035000055 Timedelta (minutely rounded)... 2 days 10:45:00
廣告