Python Pandas - 将给定的时间戳转换为频率为每小时的周期
要将给定的时间戳转换为周期,请使用timestamp.to_period()方法。其中,使用freq参数设置频率。对于每小时频率,将 freq 设置为 H。
首先,导入所需的库 −
import pandas as pd
在 Pandas 中创建时间戳对象
timestamp = pd.Timestamp(2021, 9, 18, 11, 50, 20, 33)
将时间戳转换为周期。我们已使用值“H”将“freq”参数设置了频率为每小时
timestamp.to_period(freq='H')
示例
以下是代码
import pandas as pd
# set the timestamp object in Pandas
timestamp = pd.Timestamp(2021, 9, 18, 11, 50, 20, 33)
# display the Timestamp
print("Timestamp...\n", timestamp)
# convert timestamp to Period
# we have set the frequency as hourly using the "freq" parameter with value 'H'
print("\nTimestamp to Period with hourly frequency...\n", timestamp.to_period(freq='H'))输出
这将生成以下代码
Timestamp... 2021-09-18 11:50:20.000033 Timestamp to Period with hourly frequency... 2021-09-18 11:00
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP