Python Pandas - 将给定的时间戳转换为周期
要将给定的时间戳转换为周期,请使用 timestamp.to_period() 函数。在内部,使用 freq 参数设置频率。
首先,导入必需的计算机库 −
import pandas as pd
在 Pandas 中设置时间戳对象
timestamp = pd.Timestamp('2021-09-14T15:12:34.261811624')现在,将时间戳转换为周期。我们已经使用值“M”将频率设置为月份,即“freq”参数
timestamp.to_period(freq='M')
示例
以下为代码
import pandas as pd
# set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-09-14T15:12:34.261811624')
# display the Timestamp
print("Timestamp...\n", timestamp)
# convert timestamp to Period
# we have set the frequency as Month using the "freq" parameter with value 'M'
print("\nTimestamp to Period...\n", timestamp.to_period(freq='M'))输出
这将生成以下代码
Timestamp... 2021-09-14 15:12:34.261811624 Timestamp to Period... 2021-09
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP