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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP