Python Pandas - 将 Period 对象作为每日频率的时间戳返回
要将Period对象作为具有每日频率的时间戳返回,请使用该方法并将freq参数设置为“D”。period.to_timestamp()
首先,导入所需的库-
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对象的时间戳表示。我们已经使用“freq”参数设置了频率。频率设置为“D”,即每天
print("\nPeriod to Timestamp with daily frequency...\n", period.to_timestamp(freq='D'))
示例
以下是代码
import pandas as pd #Thepandas.Periodrepresentsaperiodoftime #CreatingaPeriodobject period = pd.Period(freq="S", year = 2021, month = 11, day = 26, hour = 11, minute = 45, second = 55) #displaythePeriodobject print("Period...\n", period) #ReturntheTimestamprepresentationofthePeriodobject # 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