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


要将给定 Period 对象的频率从秒更改为天,请使用 period.asfreq() 方法并设置参数‘D’。

首先,导入所需的库 -

import pandas as pd

pandas.Period 表示一段时间。创建一个 Period 对象。我们使用 ‘freq’ 参数将频率设置为秒,即 'S'。

period = pd.Period(freq="S", year = 2021, month = 4, day = 16, hour = 2, minute = 35, second = 15)

显示以秒为频率的 Period 对象

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

将 Period 从秒转换为天。我们使用 asfreq() 将“D”设置为按天转换

res = period.asfreq('D')

示例

以下是代码

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 = 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 Daily frequency
# We have set the "D" to convert seconds to daily frequency using asfreq()
res = period.asfreq('D')

# display the result after conversion from Seconds to Daily frequency
print("\nFinal result after converting frequency ...\n", res)

输出

这将生成以下代码

Period...
2021-04-16 02:35:15

Final result after converting frequency ...
2021-04-16

更新于: 2021 年 10 月 20 日

455 次浏览

开启你的事业

完成课程获得认证

开始
广告