Python Pandas - 获取给定周期对象的频率


要获取指定周期对象的频率,请使用 period.freq 属性。

首先,导入所需的库 −

import pandas as pd

pandas.Period 表示一个时间段。创建两个 Period 对象 −

period1 = pd.Period("2020-09-23 03:55:20")
period2 = pd.Period(freq="T", year = 2021, month = 2, day = 14, hour = 2, minute = 35)

显示 Period 对象 −

print("Period1...\n", period1)
print("Period2...\n", period2)

从两个 Period 对象获取频率 −

res1 = period1.freq
res2 = period2.freq

示例

以下为代码 −

import pandas as pd

# The pandas.Period represents a period of time
# creating two Period objects
period1 = pd.Period("2020-09-23 03:55:20")
period2 = pd.Period(freq="T", year = 2021, month = 2, day = 14, hour = 2, minute = 35)

# display the Period objects
print("Period1...\n", period1)
print("Period2...\n", period2)

# get the frequency from two Period objects
res1 = period1.freq
res2 = period2.freq

# Return the frequency from two Period objects
print("\nDisplay the frequency from the 1st Period object ...\n", res1)
print("\nDisplay the frequency from the 2nd Period object ...\n", res2)

输出

这将产生以下代码 −

Period1...
2020-09-23 03:55:20
Period2...
2021-02-14 02:35

Display the frequency from the 1st Period object ...
<Second>

Display the frequency from the 2nd Period object ...
<Minute>

更新于: 14-10-2021

407 次浏览

启动你的 职业生涯

通过完成课程获得认证

开始
广告