Python Pandas - 使用季度频率将给定的时间戳转换为频率


要将给定的时间戳转换为频率,请使用 **timestamp.to_period()** 方法。其中,使用 **freq** 参数设置频率。对于季度频率,将 freq 设置为 Q。

首先,导入所需的库:

import pandas as pd

在 Pandas 中创建时间戳对象

timestamp = pd.Timestamp(2021, 9, 18, 11, 50, 20, 33)

将时间戳转换为频率。使用值 'Q' 的 "freq" 参数将频率设置为季度

timestamp.to_period(freq='Q')

示例

以下是代码

Open Compiler
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 quarterly using the "freq" parameter with value 'Q' print("\nTimestamp to Period with quarterly frequency...\n", timestamp.to_period(freq='Q'))

输出

这将生成以下代码

Timestamp...
 2021-09-18 11:50:20.000033
Timestamp to Period with quarterly frequency...
 2021Q3

更新于: 2021-10-13

550 浏览

开启你的 职业生涯

完成课程以获取认证

开始
广告