Python Pandas - 将给定的 Timestamp 转换为具有每周频率的 Period
要将给定的 Timestamp 转换为 Period,请使用 timestamp.to_period() 方法。在方法中,使用 freq 参数设置频率。对于每周频率,将 freq 设置为 W。
首先,导入所需的库 −
import pandas as pd
在 Pandas 中创建 timestamp 对象
timestamp = pd.Timestamp(2021, 9, 18, 11, 50, 20, 33)
将 timestamp 转换为 Period。我们使用值 'W' 设置了 "freq" 参数作为每周频率
timestamp.to_period(freq='W')
例子
以下为代码
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 weekly using the "freq" parameter with value 'W'
print("\nTimestamp to Period with weekly frequency...\n", timestamp.to_period(freq='W'))输出
将生成以下代码
Timestamp... 2021-09-18 11:50:20.000033 Timestamp to Period with weekly frequency... 2021-09-13/2021-09-19
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP