要将给定 Period 对象的频率从 Seconds 更改为 Hourly 频率,请使用该方法并设置参数“H”。period.asfreq()
首先,导入所需的库 -
import pandas as pd
的pandas.Period代表的一段时间。创建一个 Period 对象。我们已将频率设置为秒,即。'S' 使用 'freq' 参数
period = pd.Period(freq="S", year = 2021, month = 4, day = 16, hour = 2, minute = 35, second = 15)
以秒频率显示 Period 对象
print("Period...\n", period)
将周期从秒转换为每小时频率。我们已经设置了“H”以使用将秒数转换为小时频率asfreq()
res = period.asfreq('H')
以下是代码
import pandas as pd # Thepandas.Periodrepresents a period of time # Creating a Period object # We have set the frequency as seconds ie. 'S' using the 'freq' parameter period = pd.Period(freq="S", year = 2021, month = 4, day = 16, hour = 2, minute = 35, second = 15) # display the Period object with Seconds frequency print("Period...\n", period) # Convert Period from Seconds to Hourly frequency # We have set the "H" to convert seconds to hourly frequency using asfreq() res = period.asfreq('H') # display the result after conversion from Seconds to hourly frequency print("\nFinal result after converting frequency ...\n", res)输出结果
这将产生以下代码
Period... 2021-04-16 02:35:15 Final result after converting frequency ... 2021-04-16 02:00