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