Python Pandas - 以年频率将 Period 对象返回为时间戳
要将 Period 对象以年频率返回为时间戳,请使用 period.to_timestamp() 方法,并设置 freq 参数为 ‘Y’。
首先,导入所需的库 −
import pandas as pd
pandas.Period 表示一个时间段。创建一个 Period 对象
period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 17, minute = 20, second = 45)
显示 Period 对象
print("Period...\n", period)返回 Period 对象的时间戳表示形式。我们使用 "freq" 参数设置了频率。频率设置为 'Y',即每年
print("\nPeriod to Timestamp with yearly (year-end) frequency...\n", period.to_timestamp(freq='Y'))
示例
以下是代码
import pandas as pd
# The pandas.Period represents a period of time
# Creating a Period object
period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 17, minute = 20, second = 45)
# 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 'Y' i.e. yearly
print("\nPeriod to Timestamp with yearly (year-end) frequency...\n", period.to_timestamp(freq='Y'))输出
这将会生成以下代码
Period... 2021-09-18 17:20:45 Period to Timestamp with yearly (year-end) frequency... 2021-12-31 00:00:00
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP