Python Pandas - 返回 Period 对象的时间戳表示
要返回Period对象的时间戳表示,请使用该方法。period.to_timestamp()
首先,导入所需的库-
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”参数设置了频率
print("\nPeriod to Timestamp...\n", period.to_timestamp(freq='T'))
示例
以下是代码
import pandas as pd #Thepandas.Periodrepresentsaperiodoftime #CreatingaPeriodobject period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 17, minute = 20, second = 45) #displaythePeriodobject print("Period...\n", period) #ReturntheTimestamprepresentationofthePeriodobject # We have set the frequency using the "freq" parameter print("\nPeriod to Timestamp...\n", period.to_timestamp(freq='T'))输出结果
这将产生以下代码
Period... 2021-09-18 17:20:45 Period to Timestamp... 2021-09-18 17:20:00