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