詳解如何使用Pandas處理時(shí)間序列數(shù)據(jù)
Pandas-如何輕松處理時(shí)間序列數(shù)據(jù)
時(shí)間序列數(shù)據(jù)在數(shù)據(jù)分析建模中很常見,例如天氣預(yù)報(bào),空氣狀態(tài)監(jiān)測(cè),股票交易等金融場(chǎng)景。此處選擇巴黎、倫敦歐洲城市空氣質(zhì)量監(jiān)測(cè)NO2數(shù)據(jù)作為樣例。
監(jiān)測(cè)的時(shí)間序列數(shù)據(jù)
比如,air quality no2 數(shù)據(jù)表中,主要是巴黎,倫敦等城市的每小時(shí)環(huán)境監(jiān)測(cè)數(shù)據(jù):
In [2]: air_quality.head()
Out[2]:
city country datetime location parameter value unit
0 Paris FR 2019-06-21 00:00:00+00:00 FR04014 no2 20.0 μg/m3
1 Paris FR 2019-06-20 23:00:00+00:00 FR04014 no2 21.8 μg/m3
2 Paris FR 2019-06-20 22:00:00+00:00 FR04014 no2 26.5 μg/m3
3 Paris FR 2019-06-20 21:00:00+00:00 FR04014 no2 24.9 μg/m3
4 Paris FR 2019-06-20 20:00:00+00:00 FR04014 no2 21.4 μg/m3
In [3]: air_quality.city.unique()
Out[3]: array(['Paris', 'Antwerpen', 'London'], dtype=object)
轉(zhuǎn)換為日期時(shí)間對(duì)象
默認(rèn)讀取的日期數(shù)據(jù),實(shí)際上是字符串string 類型,無法進(jìn)行日期時(shí)間的操作,可以轉(zhuǎn)換為datetime數(shù)據(jù)對(duì)象類型,可以用to_datetime() 函數(shù)這樣操作:
In [5]: air_quality["datetime"] = pd.to_datetime(air_quality["datetime"])
In [6]: air_quality["datetime"]
Out[6]:
0 2019-06-21 00:00:00+00:00
1 2019-06-20 23:00:00+00:00
2 2019-06-20 22:00:00+00:00
3 2019-06-20 21:00:00+00:00
4 2019-06-20 20:00:00+00:00
...
2063 2019-05-07 06:00:00+00:00
2064 2019-05-07 04:00:00+00:00
2065 2019-05-07 03:00:00+00:00
2066 2019-05-07 02:00:00+00:00
2067 2019-05-07 01:00:00+00:00
Name: datetime, Length: 2068, dtype: datetime64[ns, UTC]
當(dāng)然,也可以在pandas.read_csv(), 和 pandas.read_json()函數(shù)中,直接就解析轉(zhuǎn)換為datetime類型,parse_dates 參數(shù)
pd.read_csv("../data/air_quality_no2_long.csv", parse_dates=["datetime"])
時(shí)間操作的典型問題
如何找到序列中的時(shí)間開始和時(shí)間結(jié)束?
In [7]: air_quality["datetime"].min(), air_quality["datetime"].max()
Out[7]:
(Timestamp('2019-05-07 01:00:00+0000', tz='UTC'),
Timestamp('2019-06-21 00:00:00+0000', tz='UTC'))
通過min()函數(shù),找到時(shí)間最小值,也就是開始時(shí)間;max()函數(shù),找到時(shí)間序列最大值,也就是結(jié)束時(shí)間。
如何比較兩個(gè)時(shí)間點(diǎn)?如何計(jì)算時(shí)間跨度,或者持續(xù)時(shí)間?
In [8]: air_quality["datetime"].max() - air_quality["datetime"].min()
Out[8]: Timedelta('44 days 23:00:00')
pandas.Timestamp 可以直接計(jì)算差值,結(jié)果為pandas.Timedelta 類型,類似于python庫的時(shí)間跨度,datetime.timedelta
如何僅關(guān)注某個(gè)時(shí)間單位?
比如年,月,日,比如增加一列,表示月份?
In [11]: air_quality["month"] = air_quality["datetime"].dt.month
In [12]: air_quality.head()
Out[12]:
city country datetime ... value unit month
0 Paris FR 2019-06-21 00:00:00+00:00 ... 20.0 μg/m3 6
1 Paris FR 2019-06-20 23:00:00+00:00 ... 21.8 μg/m3 6
2 Paris FR 2019-06-20 22:00:00+00:00 ... 26.5 μg/m3 6
3 Paris FR 2019-06-20 21:00:00+00:00 ... 24.9 μg/m3 6
4 Paris FR 2019-06-20 20:00:00+00:00 ... 21.4 μg/m3 6
[5 rows x 8 columns]
使用timestamp的 dt.month屬性,提取月份數(shù)值。類似的,也可以提取年,日,季節(jié)等等時(shí)間屬性。
如何計(jì)算每周,每個(gè)城市的No2濃度平均值?
In [13]: air_quality.groupby(
....: [air_quality["datetime"].dt.weekday, "location"])["value"].mean()
....:
Out[13]:
datetime location
0 BETR801 27.875000
FR04014 24.856250
London Westminster 23.969697
1 BETR801 22.214286
FR04014 30.999359
...
5 FR04014 25.266154
London Westminster 24.977612
6 BETR801 21.896552
FR04014 23.274306
London Westminster 24.859155
Name: value, Length: 21, dtype: float64
使用groupby函數(shù),按照周為時(shí)間單位,按城市分組然后合并歸集,計(jì)算組內(nèi)均值。
如何把時(shí)間Datetime作為索引?
In [18]: no_2 = air_quality.pivot(index="datetime", columns="location", values="value") In [19]: no_2.head() Out[19]: location BETR801 FR04014 London Westminster datetime 2019-05-07 01:00:00+00:00 50.5 25.0 23.0 2019-05-07 02:00:00+00:00 45.0 27.7 19.0 2019-05-07 03:00:00+00:00 NaN 50.4 19.0 2019-05-07 04:00:00+00:00 NaN 61.9 16.0 2019-05-07 05:00:00+00:00 NaN 72.4 NaN
pivot()函數(shù),指定索引 index,列屬性columns和數(shù)值values,生成二維表。
另外,也可以通過set_index函數(shù)。
如何更新頻度的重采樣?
比如,把當(dāng)前每小時(shí)的采樣頻度,更新為每個(gè)月,取最大值作為采樣值。
In [22]: monthly_max = no_2.resample("M").max()
In [23]: monthly_max
Out[23]:
location BETR801 FR04014 London Westminster
datetime
2019-05-31 00:00:00+00:00 74.5 97.0 97.0
2019-06-30 00:00:00+00:00 52.5 84.7 52.0
resample()函數(shù),類似于groupby分組聚合函數(shù)。
以上代碼只是一個(gè)簡單示例,示例代碼中的表達(dá)式可以根據(jù)實(shí)際問題進(jìn)行修改。
到此這篇關(guān)于詳解如何使用Pandas處理時(shí)間序列數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Pandas處理時(shí)間序列數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PyHacker實(shí)現(xiàn)網(wǎng)站后臺(tái)掃描器編寫指南
這篇文章主要為大家介紹了PyHacker實(shí)現(xiàn)網(wǎng)站后臺(tái)掃描器編寫指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Pytorch如何指定device(cuda or cpu)
這篇文章主要介紹了Pytorch如何指定device(cuda or cpu)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Python基礎(chǔ)之高級(jí)變量類型實(shí)例詳解
這篇文章主要介紹了Python基礎(chǔ)之高級(jí)變量類型,結(jié)合實(shí)例形式詳細(xì)分析了Python元組、字典、字符串、公共方法以及遍歷、切片等常見操作技巧,需要的朋友可以參考下2020-01-01
linux環(huán)境打包python工程為可執(zhí)行程序的過程
本次需求,在ubuntu上面開發(fā)的python代碼程序需要打包成一個(gè)可執(zhí)行程序然后交付給甲方,因?yàn)椴荒苤苯咏o源碼給甲方,所以尋找方法將python開發(fā)的源碼打包成一個(gè)可執(zhí)行程序,本次在ubuntu上打包python源碼的方法和在window上打包的有點(diǎn)類似,感興趣的朋友跟隨小編一起看看吧2024-01-01
python在多玩圖片上下載妹子圖的實(shí)現(xiàn)代碼
學(xué)python的第二天,想寫個(gè)東西出來玩玩,于是就寫了這個(gè),供那些才學(xué)一天的參考參考也行2013-08-08

