天天看點

Pandas的時間序列Period,period_range---詳解(29)

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資料類型的屬性有:

day

Get day of the month that a Period falls on.

dayofweek

Return the day of the week.

dayofyear

Return the day of the year.

days_in_month

Get the total number of days in the month that this period falls on.

daysinmonth

Get the total number of days of the month that the Period falls in.

hour

Get the hour of the day component of the Period.

minute

Get minute of the hour component of the Period.

second

Get the second component of the Period.

start_time

Get the Timestamp for the start of the period.

week

Get 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