Python Pandas - 使用每日频率舍入 Timedelta
要使用指定的精度舍入 Timedelta,可以使用 timestamp.round() 方法。使用值 'D' 设置使用 freq 参数的每日频率精度。
首先,导入所需的库 −
import pandas as pd
TimeDeltas 是 Python 的标准 datetime 库,它使用不同的表示形式 timedelta。创建一个 Timedelta 对象 −
timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns')
显示 Timedelta −
print("Timedelta...\n", timedelta)返回具有每日频率的舍入时间戳。在此,使用 "freq" 参数设置指定的精度 −
timedelta.round(freq='D')
示例
以下是代码 −
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 11 hours 22 min 25 s 50 ms 45 ns')
# display the Timedelta
print("Timedelta...\n", timedelta)
# return the rounded Timestamp
# with daily frequency
# Here, the specified resolution is set using the "freq" parameter
res = timedelta.round(freq='D')
# display the rounded Timestamp
print("\nTimedelta (daily rounded)...\n", res)输出
这将产生以下代码 −
Timedelta... 2 days 11:22:25.050000045 Timedelta (daily rounded)... 2 days 00:00:00
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP