天天看點

Python資料分析實戰【第三章】2.11- Pandas時期:Period【python】1.pd.Period()建立時期2.pd.period_range()建立時期範圍3.asfreq:頻率轉換4.時間戳與時期之間的轉換:pd.to_period()、pd.to_timestamp()

【課程2.11】 Pandas時期:Period

核心:pd.Period()

1.pd.Period()建立時期

p = pd.Period('2017', freq = 'M')
print(p, type(p))
# 生成一個以2017-01開始,月為頻率的時間構造器
# pd.Period()參數:一個時間戳 + freq 參數 → freq 用于指明該 period 的長度,時間戳則說明該 period 在時間軸上的位置

print(p + 1)
print(p - 2)
print(pd.Period('2012', freq = 'A-DEC') - 1)
# 通過加減整數,将周期整體移動
# 這裡是按照 月、年 移動
-----------------------------------------------------------------------
2017-01 <class 'pandas._period.Period'>
2017-02
2016-11
2011
           

2.pd.period_range()建立時期範圍

prng = pd.period_range('1/1/2011', '1/1/2012', freq='M')
print(prng,type(prng))
print(prng[0],type(prng[0]))
# 資料格式為PeriodIndex,單個數值為Period

ts = pd.Series(np.random.rand(len(prng)), index = prng)
print(ts,type(ts))
print(ts.index)
# 時間序列

# Period('2011', freq = 'A-DEC')可以看成多個時間期的時間段中的遊标
# Timestamp表示一個時間戳,是一個時間截面;Period是一個時期,是一個時間段!!但兩者作為index時差別不大
-----------------------------------------------------------------------
PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04', '2011-05', '2011-06',
             '2011-07', '2011-08', '2011-09', '2011-10', '2011-11', '2011-12',
             '2012-01'],
            dtype='int64', freq='M') <class 'pandas.tseries.period.PeriodIndex'>
2011-01 <class 'pandas._period.Period'>
2011-01    0.342571
2011-02    0.826151
2011-03    0.370505
2011-04    0.137151
2011-05    0.679976
2011-06    0.265928
2011-07    0.416502
2011-08    0.874078
2011-09    0.112801
2011-10    0.112504
2011-11    0.448408
2011-12    0.851046
2012-01    0.370605
Freq: M, dtype: float64 <class 'pandas.core.series.Series'>
PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04', '2011-05', '2011-06',
             '2011-07', '2011-08', '2011-09', '2011-10', '2011-11', '2011-12',
             '2012-01'],
            dtype='int64', freq='M')
           

3.asfreq:頻率轉換

p = pd.Period('2017','A-DEC')
print(p)
print(p.asfreq('M', how = 'start'))  # 也可寫 how = 's'
print(p.asfreq('D', how = 'end'))  # 也可寫 how = 'e'
# 通過.asfreq(freq, method=None, how=None)方法轉換成别的頻率

prng = pd.period_range('2017','2018',freq = 'M')
ts1 = pd.Series(np.random.rand(len(prng)), index = prng)
ts2 = pd.Series(np.random.rand(len(prng)), index = prng.asfreq('D', how = 'start'))
print(ts1.head(),len(ts1))
print(ts2.head(),len(ts2))
# asfreq也可以轉換TIMESeries的index
-----------------------------------------------------------------------
2017
2017-01
2017-12-31
2017-01    0.060797
2017-02    0.441994
2017-03    0.971933
2017-04    0.000334
2017-05    0.545191
Freq: M, dtype: float64 13
2017-01-01    0.447614
2017-02-01    0.679438
2017-03-01    0.891729
2017-04-01    0.949993
2017-05-01    0.942548
Freq: D, dtype: float64 13
           

4.時間戳與時期之間的轉換:pd.to_period()、pd.to_timestamp()

rng = pd.date_range('2017/1/1', periods = 10, freq = 'M')
prng = pd.period_range('2017','2018', freq = 'M')

ts1 = pd.Series(np.random.rand(len(rng)), index = rng)
print(ts1.head())
print(ts1.to_period().head())
# 每月最後一日,轉化為每月

ts2 = pd.Series(np.random.rand(len(prng)), index = prng)
print(ts2.head())
print(ts2.to_timestamp().head())
# 每月,轉化為每月第一天
-----------------------------------------------------------------------
2017-01-31    0.125288
2017-02-28    0.497174
2017-03-31    0.573114
2017-04-30    0.665665
2017-05-31    0.263561
Freq: M, dtype: float64
2017-01    0.125288
2017-02    0.497174
2017-03    0.573114
2017-04    0.665665
2017-05    0.263561
Freq: M, dtype: float64
2017-01    0.748661
2017-02    0.095891
2017-03    0.280341
2017-04    0.569813
2017-05    0.067677
Freq: M, dtype: float64
2017-01-01    0.748661
2017-02-01    0.095891
2017-03-01    0.280341
2017-04-01    0.569813
2017-05-01    0.067677
Freq: MS, dtype: float64