首页 文章资讯内容详情

Python Pandas - 将给定 Period 对象的频率从秒更改为分钟频率

2026-06-03 1 花语

要将给定Period对象的频率从Seconds更改为Minutely频率,请使用该方法并设置参数Tperiod.asfreq()

首先,导入所需的库-

import pandas as pd

的pandas.Period代表的一段时间。创建一个Period对象。我们使用“freq”参数将频率设置为秒,即“S”

period = pd.Period(freq="S", year = 2021, month = 9, day = 11, hour = 8, minute = 20, second = 45)

以秒频率显示Period对象

print("Period...\n", period)

将周期从秒转换为分钟频率。我们已经设置了“T”以使用将秒数转换为分钟频率asfreq()

res = period.asfreq(T)

示例

以下是代码

import pandas as pd #Thepandas.Periodrepresentsaperiodoftime #CreatingaPeriodobject # We have set the frequency as seconds ie. S using the freq parameter period = pd.Period(freq="S", year = 2021, month = 9, day = 11, hour = 8, minute = 20, second = 45) #displaythePeriodobjectwithSecondsfrequency print("Period...\n", period) #ConvertPeriodfromSecondstoMinutelyfrequency # We have set the "T" to convert seconds to minutely frequency using asfreq() res = period.asfreq(T) #displaytheresultafterconversionfromSecondstoMinutelyfrequency print("\nFinal result after converting frequency ...\n", res)输出结果

这将产生以下代码

Period... 2021-09-11 08:20:45 Final result after converting frequency ... 2021-09-11 08:20