Pandas 時(shí)間序列之 Period
1. 前言
上一小節(jié)我們學(xué)習(xí)了 Pandas 時(shí)間序列的第一種類型,時(shí)間戳(Timestamp),表示的是一個(gè)特定的時(shí)間點(diǎn),除此之外,時(shí)間序列還有一種時(shí)間時(shí)期 (Period)類型,它表示的是時(shí)間區(qū)間,比如數(shù)日、數(shù)月、數(shù)年等數(shù)據(jù),這小節(jié)我們開始學(xué)習(xí) Period 類型的創(chuàng)建和使用方法。
2. 時(shí)期 Period
2.1 Period 的創(chuàng)建方法
Pandas 庫中提供了特定的函數(shù)用于創(chuàng)建 Period 時(shí)期數(shù)據(jù)。其中提供了豐富參數(shù)邊便于靈活的配置:
pd.Period(value=None, freq=None, ordinal=None, year=None, month=None,quarter=None, day=None, hour=None, minute=None, second=None)
參數(shù)名 | 說明 |
---|---|
value | 要轉(zhuǎn)換為時(shí)期的值 |
freq | 時(shí)期的移動頻率 |
year、month、quarter、day、hour | 年、月、季、日、時(shí) |
下面我們通過代碼展示具體的創(chuàng)建方法:
# 導(dǎo)入 pandas 數(shù)據(jù)包
import pandas as pd
# 創(chuàng)建時(shí)期對象
period_res = pd.Period('2020-12', freq = 'D')
print(period_res)
# --- 輸出結(jié)果 ---
2020-12-01
# 結(jié)果解析:這里可以看到,我們通過指定 “2020-12” 和 頻率 "D" 表示 day 的縮寫,輸出的結(jié)果是 2020-12-01 的時(shí)期。對于頻率的理解,我們可以理解為移動的單位,下面我們通過運(yùn)算看一下,freq 的效果:
print(period_res+3)
# --- 輸出結(jié)果 ---
2017-12-04
2.2 時(shí)期常用的屬性和方法
Period 中提供了豐富的屬性和方法供用戶使用,方便了對時(shí)期數(shù)據(jù)的處理和分析,下面我們列舉一些常用的屬性和方法,并用代碼進(jìn)行應(yīng)用演示。
1. start_time 和 end_time 屬性
這兩個(gè)屬性分別用于獲取時(shí)期數(shù)據(jù)對象的開始時(shí)間和結(jié)束時(shí)間,返回的值是時(shí)間戳。
# 導(dǎo)入 pandas 數(shù)據(jù)包
import pandas as pd
period_res = pd.Period('2017-12', freq = '2D')
print(period_res.start_time)
# --- 輸出結(jié)果 ---
2017-12-01 00:00:00
print(period_res.end_time)
# --- 輸出結(jié)果 ---
2017-12-02 23:59:59.999999999
2. freq 和 freqstr 屬性
該屬性用于獲取時(shí)期對象的頻率,只不過 freqstr 屬性獲取的是縮寫的字母。
# 導(dǎo)入 pandas 數(shù)據(jù)包
import pandas as pd
period_res = pd.Period('2017-12', freq = '2D')
print(period_res.freq)
# --- 輸出結(jié)果 ---
<2 * Days>
print(period_res.freqstr)
# --- 輸出結(jié)果 ---
2D
結(jié)果解析:這里要注意的是在我們的頻率前面還有一個(gè) 數(shù)字 2,因?yàn)槲覀冊O(shè)置的是頻率是 2D,表示兩天為單位。
3. year 、month 和 day 屬性
這三個(gè)屬性分別用于獲取時(shí)期的哪一年,一年中的第幾個(gè)月,一個(gè)月中的第幾天。
# 導(dǎo)入 pandas 數(shù)據(jù)包
import pandas as pd
period_res = pd.Period('2017-12', freq = '2D')
print(period_res.year)
print(period_res.month)
print(period_res.day)
# --- 輸出結(jié)果 ---
2017 # 年
12 #月
1 #日
4. weekofyear 和 dayofweek 屬性
這兩個(gè)屬性分別用于獲取時(shí)期是一年中的第幾周,一周中的周幾(0 表示周 1)。
# 導(dǎo)入 pandas 數(shù)據(jù)包
import pandas as pd
period_res = pd.Period('2020-12-4', freq = '2D')
print(period_res.weekofyear)
print(period_res.dayofweek)
# --- 輸出結(jié)果 ---
49 # 2020年的第49周
4 # 從0開始,4表示周五
5. now () 方法
獲取當(dāng)前的時(shí)間,可以通過指定方法中的頻率,獲取當(dāng)前時(shí)期對象。
# 導(dǎo)入 pandas 數(shù)據(jù)包
import pandas as pd
period_res = pd.Period('2020-12-4', freq = '3D')
print(period_res.now(freq='D'))
# --- 輸出結(jié)果 ---
2021-01-11
6. asfreq () 方法
該方法用于時(shí)期頻率的轉(zhuǎn)換。
# 導(dǎo)入 pandas 數(shù)據(jù)包
import pandas as pd
period_res = pd.Period('2020-12', freq = 'M')
print(period_res)
print(period_res.asfreq('D'))
# --- 輸出結(jié)果 ---
2020-12 # 年為頻率的時(shí)期
2020-12-31 # 天為頻率的時(shí)期,由大頻率轉(zhuǎn)為小頻率時(shí),默認(rèn)是大頻率時(shí)期的末尾數(shù)據(jù)進(jìn)行轉(zhuǎn)換,這里就是12月的最后一天轉(zhuǎn)換為頻率為 D 的數(shù)據(jù),我們還可以通過 how='start' 來指定轉(zhuǎn)換時(shí)的依據(jù)(默認(rèn) how='end'):
print(period_res.asfreq('D',how = 'start'))
# --- 輸出結(jié)果 ---
2020-12-01
2.3 時(shí)期索引
Period 中的索引類型為 PeriodIndex,我們可以通過函數(shù) period_range () 生成時(shí)期序列,作為其他數(shù)據(jù)集的索引。
# 導(dǎo)入 pandas 數(shù)據(jù)包
import pandas as pd
period_index_res = pd.period_range('2020-02-22', '2020-03-04', freq='2D')
print(period_index_res)
# --- 輸出結(jié)果 ---
PeriodIndex(['2020-02-22', '2020-02-24', '2020-02-26', '2020-02-28',
'2020-03-01', '2020-03-03'],dtype='period[2D]', freq='2D')
# 結(jié)果解析:這里可以看到生成的 PeriodIndex 序列值,我們下面將用他作為索引生成生產(chǎn) DataFrame 數(shù)據(jù)集。
value_dateDataFrame = [["aa","bb"],["aa","bb"],["aa","bb"],["aa","bb"],["aa","bb"],["aa","bb"]]
res = pd.DataFrame(value_dateDataFrame, index = period_index_res)
print(res)
# --- 輸出結(jié)果 ---
0 1
2020-02-22 aa bb
2020-02-24 aa bb
2020-02-26 aa bb
2020-02-28 aa bb
2020-03-01 aa bb
2020-03-03 aa bb
# 結(jié)果解析:這里可看到 時(shí)期序列作為索引的數(shù)據(jù)結(jié)果,可以方便的通過年、月去獲取數(shù)據(jù)。
3. 小結(jié)
本節(jié)課程我們主要學(xué)習(xí)了 Pandas 時(shí)間序列的 Period ,包括時(shí)期數(shù)據(jù)對象的創(chuàng)建,常用的屬性和方法,以及時(shí)期索引在數(shù)據(jù)集中的具體應(yīng)用。本節(jié)課程的重點(diǎn)如下:
- Period 時(shí)期數(shù)據(jù)創(chuàng)建函數(shù) Period () 的使用;
- Period 時(shí)期對象常用屬性和方法的應(yīng)用;
- Period 時(shí)期索引的應(yīng)用。