Python Pandas - 将 Period 对象作为时间戳返回,粒度为分钟


要将 Period 对象返回为频率为每月的 timestamp,请使用 period.to_timestamp() 方法,并将 freq 参数设置为 'T'。

首先,导入所需的库 -

import pandas as pd

pandas.Period 表示一段时期。创建 Period 对象

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

显示 Period 对象

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

返回 Period 对象的 Timestamp 表示。我们使用“freq”参数设置了频率。频率设置为 'T',即分钟

print("\nPeriod to Timestamp with minutely frequency...\n", period.to_timestamp(freq='T'))

示例

以下是代码

import pandas as pd

# The pandas.Period represents a period of time
# Creating a Period object
period = pd.Period(freq="S", year = 2021, month = 11, day = 26, hour = 11, minute = 45, second = 55)

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

# Return the Timestamp representation of the Period object
# We have set the frequency using the "freq" parameter
# The frequency is set as 'T' i.e. monthly
print("\nPeriod to Timestamp with minutely frequency...\n", period.to_timestamp(freq='T'))

输出

这将生成以下代码

Period...
2021-11-26 11:45:55

Period to Timestamp with minutely frequency...
2021-11-26 11:45:00

已更新:2021 年 10 月 20 日

63 次浏览

开启您的职业生涯

通过完成课程获得认证

开始
广告