Pandas Python - 返回频率为每天的时间戳作为周期对象


若要将周期对象作为时间戳返回,并以每日频率显示,请使用 period.to_timestamp() 方法,并将 freq 参数设置为“D”。

首先,导入必要的库 -

import pandas as pd

表示一段时间间隔的 pandas.Period。创建一个周期对象

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

显示周期对象

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

返回周期对象的时间戳表示形式。我们已使用“freq”参数设置频率。频率设置为“D”,即每天

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

示例

以下为代码

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 'D' i.e. daily
print("\nPeriod to Timestamp with daily frequency...\n", period.to_timestamp(freq='D'))

输出

这将生成以下代码

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

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

更新于:2021-10-20

161 次浏览

开启你的职业生涯

通过完成课程取得认证

开始
广告
© . All rights reserved.