Pandas 時(shí)間序列之 Timestamp
1. 前言
上一小節(jié),我們了解了 Pandas 庫(kù)中時(shí)間序列的特點(diǎn)和三類常用時(shí)間序列的類型,從本小節(jié)開始,我們將針對(duì)每一類時(shí)間序列展開詳細(xì)的學(xué)習(xí),本小節(jié)我們將學(xué)習(xí)時(shí)間序列之時(shí)間戳 (Timestamp) 的詳細(xì)特點(diǎn)和創(chuàng)建方法。
2. 時(shí)間戳
時(shí)間戳表示特定的時(shí)間點(diǎn),在 Pandas 中的提供了時(shí)間戳數(shù)據(jù)類型 Timestamp 。
2.1 時(shí)間戳的創(chuàng)建方法
Pandas 庫(kù)中提供了函數(shù)供時(shí)間戳的創(chuàng)建,其中有幾個(gè)常用參數(shù),我們這在這里列舉一下:
pd.Timestamp(ts_input, freq=None, tz=None, unit=None,year=None, month=None, day=None, hour=None, minute=None,second=None, microsecond=None, nanosecond=None, tzinfo=None)
參數(shù)名 | 說明 |
---|---|
ts_input | 要轉(zhuǎn)換為時(shí)間戳的值 |
tz | 時(shí)區(qū),如 tz=‘Asia/Shanghai’ 上海時(shí)區(qū) |
year、month、day | 年、月、日 |
hour、minute、second、microsecond、nanosecond | 時(shí)、分、秒、微秒、納秒 |
下面我們通過代碼展示一下 Timestamp () 函數(shù)創(chuàng)建時(shí)間戳的操作:
# 導(dǎo)入 pandas 數(shù)據(jù)包
import pandas as pd
date_res=pd.Timestamp("2020-12-23")
print(date_res)
print(type(date_res))
# --- 輸出結(jié)果 ---
2020-12-23 00:00:00
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
# 結(jié)果解析:這里我們通過直接傳一個(gè)字符串的日期數(shù)據(jù),Timestamp() 函數(shù)會(huì)創(chuàng)建一個(gè)時(shí)間戳數(shù)據(jù)
date_res=pd.Timestamp(year=2021,month=1,day=3,hour=12,minute=23,second=22)
print(date_res)
print(type(date_res))
# --- 輸出結(jié)果 ---
2021-01-03 12:00:22
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
# 結(jié)果解析:這里我們通過指定年月日時(shí)分秒,生成一個(gè)時(shí)間戳,如果我們不指定對(duì)應(yīng)的數(shù)值,默認(rèn)是 0
2.2 時(shí)間戳常用的屬性和方法
Pandas 時(shí)間戳對(duì)象中提供了豐富的屬性,便于對(duì)時(shí)間戳的處理,這里我們通過代碼具體演示幾個(gè)常用的屬性和方法。
1. weekofyear 屬性
獲取該時(shí)間戳在當(dāng)年的第多少周
date_res=pd.Timestamp("2020-3-17",tz='Asia/Shanghai')
print(date_res.weekofyear)
# --- 輸出結(jié)果 ---
12
2. dayofyear 屬性
獲取該時(shí)間是一年的第多少天
date_res=pd.Timestamp("2021-1-6",tz='Asia/Shanghai')
print(date_res.dayofyear)
# --- 輸出結(jié)果 ---
6
3. is_year_end 屬性
獲取該時(shí)間戳是否是一年的最后一天
date_res=pd.Timestamp("2021-1-6",tz='Asia/Shanghai')
print(date_res.is_year_end)
# --- 輸出結(jié)果 ---
False
4. now() 方法
獲取當(dāng)前時(shí)間的方法
date_res=pd.Timestamp("2020-3-17",tz='Asia/Shanghai')
print(date_res.now())
# --- 輸出結(jié)果 ---
2021-01-06 21:30:08.051878
5. day_name() 方法
獲取時(shí)間戳是星期幾名字的方法
date_res=pd.Timestamp("2021-1-6",tz='Asia/Shanghai')
print(date_res.day_name())
# --- 輸出結(jié)果 ---
Wednesday
6. isocalendar() 方法
獲取時(shí)間戳是那一年,這一年的第幾周,周幾這三個(gè)值
date_res=pd.Timestamp("2021-1-6",tz='Asia/Shanghai')
print(date_res.isocalendar())
# --- 輸出結(jié)果 ---
(2021, 1, 3)
2.3 時(shí)間戳索引
上面我講到的時(shí)間戳,在放到數(shù)據(jù)集中,我們可以將其作為時(shí)間戳索引,便于我們?cè)跁r(shí)間基礎(chǔ)上對(duì)數(shù)據(jù)的處理和分析,時(shí)間戳作為索引序列對(duì)應(yīng)的對(duì)應(yīng)的類型是 DatetimeIndex ,它可以用于 Series 和 DataFrame 的數(shù)據(jù)集中。下面我們通過構(gòu)造一組 Series 數(shù)據(jù),來進(jìn)行演示創(chuàng)建時(shí)間索引 DatetimeIndex 和時(shí)間索引數(shù)據(jù)的查找特點(diǎn):
# 導(dǎo)入 pandas 數(shù)據(jù)包
import pandas as pd
df_data=pd.Series([786,890,977,912,825,586], index=['2017-04-12','2017-05-17','2018-04-12','2020-05-16','2018-05-12','2020-04-12'])
print(df_data)
# --- 輸出結(jié)果 ---
2017-04-12 786
2017-05-17 890
2018-04-12 977
2020-05-16 912
2018-05-12 825
2020-04-12 586
dtype: int64
輸出解析:這里看到我們的數(shù)據(jù)索引是一組年月日的數(shù)據(jù)格式,但他們的數(shù)據(jù)類型并不是時(shí)間序列,而是字符串。這里我們要用到 DatetimeIndex () 函數(shù)將一組數(shù)據(jù)轉(zhuǎn)換為時(shí)間戳索引:
# 通過 DatetimeIndex() 函數(shù) 轉(zhuǎn)換為時(shí)間戳索引
index_date=pd.DatetimeIndex(['2017-04-12','2017-05-17','2018-04-12','2020-05-16',
'2018-05-12','2020-04-12'])
df_data=pd.Series([786,890,977,912,825,586],index=index_date)
print(df_data)
# --- 輸出結(jié)果 ---
2017-04-12 786
2017-05-17 890
2018-04-12 977
2020-05-16 912
2018-05-12 825
2020-04-12 586
dtype: int64
# 結(jié)果解析:在輸出結(jié)果上和之前沒有什么區(qū)別,但這里我們能方便的對(duì)數(shù)據(jù)的時(shí)間索引進(jìn)行查找操作,可以直接獲得某一年的數(shù)據(jù):
print(df_data['2017'])
# --- 輸出結(jié)果 ---
2017-04-12 786
2017-05-17 890
dtype: int64
除了上面通過 DatetimeIndex () 函數(shù)創(chuàng)建時(shí)間戳索引,我們還可以通過 date_range () 函數(shù)來生成時(shí)間戳索引,具體操作如下:
# 導(dǎo)入 pandas 數(shù)據(jù)包
import pandas as pd
index_date=pd.date_range(start="2020-12-20 01:00:00",end="2020-12-20 12:00:00",freq="2H")
series_data=pd.Series([786,890,977,912,825,586],index=index_date)
print(series_data)
# --- 輸出結(jié)果 ---
2020-12-20 01:00:00 786
2020-12-20 03:00:00 890
2020-12-20 05:00:00 977
2020-12-20 07:00:00 912
2020-12-20 09:00:00 825
2020-12-20 11:00:00 586
Freq: 2H, dtype: int64
3. 小結(jié)
本節(jié)課程我們主要學(xué)習(xí)了 Pandas 庫(kù)中的時(shí)間戳,表示具體的時(shí)間點(diǎn),學(xué)習(xí)如何創(chuàng)建一個(gè)時(shí)間戳,時(shí)間戳中常用的方法和屬性,以及時(shí)間戳索引的相關(guān)知識(shí)。本節(jié)課程的重點(diǎn)如下:
- 創(chuàng)建時(shí)間戳,以及時(shí)間戳中常用的方法和屬性;
- 時(shí)間戳索引的特點(diǎn),以及如何將時(shí)間數(shù)據(jù)轉(zhuǎn)換為時(shí)間戳索引。