Python Pandas Period.to_timestamp
Python是一种进行数据分析的伟大语言,主要是因为以数据为中心的Python包的奇妙生态系统。Pandas就是这些包中的一个,它使导入和分析数据变得更加容易。
Pandas Period.to_timestamp()函数返回周期在目标频率下的时间戳表示,并指定该周期的结束时间。
语法 :
Period.to_timestamp()
参数 :
freq :目标频率。如果self.freq是一周或更长,默认为’D’,否则为’S’。
how: ‘S’, ‘E’.可别名为不区分大小写的’开始’、’结束’、’开始’、’结束’
返回:时间戳
示例#1:使用Period.to_timestamp()函数将给定的周期对象作为指定频率的Timestamp对象返回。
# importing pandas as pd
import pandas as pd
# Create the Period object
prd = pd.Period(freq ='S', year = 2000, month = 2, day = 22,
hour = 8, minute = 21, second = 24)
# Print the Period object
print(prd)
输出 :

现在我们将使用Period.to_timestamp()函数将给定的周期对象返回为一个时间戳对象。
# return as a timestamp in the specified frequency.
# 'M' represents monthly frequency
prd.to_timestamp(freq ='M')
输出 :

正如我们在输出中看到的,Period.to_timestamp()函数已经将给定的周期对象作为指定频率的时间戳返回。
例子2:使用Period.to_timestamp()函数将给定的周期对象作为指定频率的Timestamp对象返回。
# importing pandas as pd
import pandas as pd
# Create the Period object
prd = pd.Period(freq ='S', year = 2006, month = 10,
hour = 15, minute = 49, second = 17)
# Print the object
print(prd)
输出 :

现在我们将使用Period.to_timestamp()函数将给定的周期对象返回为一个时间戳对象。
# return as a timestamp in the specified frequency.
# 'T' represents minutely frequency
prd.to_timestamp(freq ='T')
输出 :

正如我们在输出中看到的,Period.to_timestamp()函数已经将给定的周期对象作为指定频率的时间戳返回。
极客教程