python Pandas - 按小時舍入 Timedelta
要透過指定的解析度舍入 Timedelta,請使用 **timestamp.round()** 方法。使用值 H 設定 hourly(小時)頻率解析度的 freq 引數。
首先要匯入所需庫 -
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)返回具有 hourly(小時)頻率的舍入時間戳。這裡,使用 "freq" 引數設定指定的解析度
timedelta.round(freq='H')
示例
以下是程式碼
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 hourly frequency
# Here, the specified resolution is set using the "freq" parameter
res = timedelta.round(freq='H')
# display the rounded Timestamp
print("\nTimedelta (hourly rounded)...\n", res)輸出
將生成以下程式碼
Timedelta... 2 days 10:45:20.035000055 Timedelta (hourly rounded)... 2 days 11:00:00
廣告
資料結構
網路
RDBMS
作業系統
java
iOS
HTML
CSS
Android
python
C 程式設計
C++
C#
MongoDB
MySQL
javascript
PHP