Pandas的时间序列Period,period
列出教育背景时,时间倒序排列 #生活技巧# #工作学习技巧# #个人简历撰写技巧#
PeriodPandas的Period可以定义一个时期,或者说具体的一个时段。有这个时段的起始时间start_time、终止时间end_time等属性信息,其参数freq和之前的date_range里的freq参数类似,可以取'S'、'D'等。
import pandas as pd
p = pd.Period('2018-12-15', freq = "A")
print p.start_time, p.end_time, p + 1, p
print pd.Period('2013-1-9 11:22:33', freq='S') + 1
print pd.Period('2013-1-9 11:22:33', freq='T') + 1
print pd.Period('2013-1-9 11:22:33', freq='H') + 1
print pd.Period('2013-1-9 11:22:33', freq='D') + 1
print pd.Period('2013-1-9 11:22:33', freq='M') + 1
print pd.Period('2013-1-9 11:22:33', freq='A') + 1
程序的执行结果如下:
2018-12-01 00:00:00 2018-12-31 23:59:59.999999999 2019-01
2018-01-01 00:00:00 2018-12-31 23:59:59.999999999 2019 2018
2013-01-09 11:22:34 # S 秒
2013-01-09 11:23 # T 分
2013-01-09 12:00 # H 时
2013-01-10 # D 天
2013-02 # M 月
2014 # A 年
Period数据类型的属性有:
dayGet day of the month that a Period falls on.dayofweekReturn the day of the week.dayofyearReturn the day of the year.days_in_monthGet the total number of days in the month that this period falls on.daysinmonthGet the total number of days of the month that the Period falls in.hourGet the hour of the day component of the Period.minuteGet minute of the hour component of the Period.secondGet the second component of the Period.start_timeGet the Timestamp for the start of the period.weekGet the week of the year on the given Period.下面可以编写程序使用一下这些属性。
import pandas as pd
att = ["S", "T", "H", "D", "M", "A"]
for a in att:
p = pd.Period('2018-12-19 11:22:33', freq= a)
print "freq =", a
print "Start from:", p.start_time, " End at:", p.end_time
print "Day",p.day, "Dayofweek", p.dayofweek,"dayofyear", p.dayofyear,"daysinmonth", p.daysinmonth
print "hour", p.hour, "minute", p.minute, "second", p.second, "\n"
程序的执行结果:
freq = S
Start from: 2018-12-19 11:22:33 End at: 2018-12-19 11:22:33.999999999
Day 19 Dayofweek 2 dayofyear 353 daysinmonth 31
hour 11 minute 22 second 33
freq = T
Start from: 2018-12-19 11:22:00 End at: 2018-12-19 11:22:59.999999999
Day 19 Dayofweek 2 dayofyear 353 daysinmonth 31
hour 11 minute 22 second 0
freq = H
Start from: 2018-12-19 11:00:00 End at: 2018-12-19 11:59:59.999999999
Day 19 Dayofweek 2 dayofyear 353 daysinmonth 31
hour 11 minute 0 second 0
freq = D
Start from: 2018-12-19 00:00:00 End at: 2018-12-19 23:59:59.999999999
Day 19 Dayofweek 2 dayofyear 353 daysinmonth 31
hour 0 minute 0 second 0
freq = M
Start from: 2018-12-01 00:00:00 End at: 2018-12-31 23:59:59.999999999
Day 31 Dayofweek 0 dayofyear 365 daysinmonth 31
hour 0 minute 0 second 0
freq = A
Start from: 2018-01-01 00:00:00 End at: 2018-12-31 23:59:59.999999999
Day 31 Dayofweek 0 dayofyear 365 daysinmonth 31
hour 0 minute 0 second 0
period_range可以通过pandas的period_range函数产生时间序列作为series的index。
import pandas as pd
import numpy as np
att = ["S", "T", "H", "D", "M", "A"]
vi = np.random.randn(5)
for a in att:
pi = pd.period_range('2018-12-19 11:22:33', periods = 5, freq= a)
ts = pd.Series(vi, index = pi)
print ts, "\n"
程序的执行结果:
2018-12-19 11:22:33 -0.275161
2018-12-19 11:22:34 -0.763390
2018-12-19 11:22:35 -2.012351
2018-12-19 11:22:36 -1.126492
2018-12-19 11:22:37 0.843842
Freq: S, dtype: float64
2018-12-19 11:22 -0.275161
2018-12-19 11:23 -0.763390
2018-12-19 11:24 -2.012351
2018-12-19 11:25 -1.126492
2018-12-19 11:26 0.843842
Freq: T, dtype: float64
2018-12-19 11:00 -0.275161
2018-12-19 12:00 -0.763390
2018-12-19 13:00 -2.012351
2018-12-19 14:00 -1.126492
2018-12-19 15:00 0.843842
Freq: H, dtype: float64
2018-12-19 -0.275161
2018-12-20 -0.763390
2018-12-21 -2.012351
2018-12-22 -1.126492
2018-12-23 0.843842
Freq: D, dtype: float64
2018-12 -0.275161
2019-01 -0.763390
2019-02 -2.012351
2019-03 -1.126492
2019-04 0.843842
Freq: M, dtype: float64
2018 -0.275161
2019 -0.763390
2020 -2.012351
2021 -1.126492
2022 0.843842
Freq: A-DEC, dtype: float64
网址:Pandas的时间序列Period,period https://www.yuejiaxmz.com/news/view/239609
相关内容
时间序列之季节性求高手修改此通达信公式虚拟成交量:IF(PERIOD=1,5,I 爱问知识人
NumPy和Pandas总结
15种时间序列预测方法总结(包含多种方法代码实现)
Java提醒接口:轻松掌握时间管理与效率提升技巧揭秘
timetask定时任务
python数据分析
利用Python进行数据分析——Pandas(2)
大学生如何管理时间英语作文
建筑节能工资料管理.doc