Python Pandas - 使用秒频舍入 Timedelta
要以指定的精度舍入 Timedelta,请使用 timestamp.round() 方法。使用值 ‘s’ 为 freq 参数设置秒频率精度。
首先,导入必需的库 −
import pandas as pd
创建 Timedelta 对象 −
timedelta = pd.Timedelta('1 days 11 hours 22 min 25 s 50 ms 45 ns')
显示 Timedelta −
print("Timedelta...\n", timedelta)
返回舍入后的时间戳,精度为秒。此处,使用 "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('1 days 11 hours 22 min 25 s 50 ms 45 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... 1 days 11:22:25.050000045 Timedelta (seconds rounded)... 1 days 11:22:25
广告