admin管理员组

文章数量:1595877

Period

Pandas的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 

 

 

本文标签: 序列详解时间pandasperiodrange