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


要将给定 Period 对象的频率从秒更改为分钟频率,请使用 **period.asfreq()** 方法并设置参数 **'T'**。

首先,导入所需的库 -

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)

将 Period 从秒转换为分钟频率。我们使用 asfreq() 设置 "T" 将秒转换为分钟频率

res = period.asfreq('T')

示例

以下是代码

import pandas as pd

# The pandas.Period represents 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 = 9, day = 11, hour = 8, minute = 20, second = 45)

# display the Period object with Seconds frequency
print("Period...\n", period)

# Convert Period from Seconds to Minutely frequency
# We have set the "T" to convert seconds to minutely frequency using asfreq()
res = period.asfreq('T')

# display the result after conversion from Seconds to Minutely frequency
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

更新于: 2021年10月20日

142 次查看

开启您的 职业生涯

通过完成课程获得认证

开始
广告