詳解如何使用Pandas處理時間序列數(shù)據(jù)
Pandas-如何輕松處理時間序列數(shù)據(jù)
時間序列數(shù)據(jù)在數(shù)據(jù)分析建模中很常見,例如天氣預(yù)報,空氣狀態(tài)監(jiān)測,股票交易等金融場景。此處選擇巴黎、倫敦歐洲城市空氣質(zhì)量監(jiān)測NO2數(shù)據(jù)作為樣例。
監(jiān)測的時間序列數(shù)據(jù)
比如,air quality no2 數(shù)據(jù)表中,主要是巴黎,倫敦等城市的每小時環(huán)境監(jiān)測數(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)換為日期時間對象
默認(rèn)讀取的日期數(shù)據(jù),實際上是字符串string 類型,無法進(jìn)行日期時間的操作,可以轉(zhuǎn)換為datetime數(shù)據(jù)對象類型,可以用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"])
時間操作的典型問題
如何找到序列中的時間開始和時間結(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ù),找到時間最小值,也就是開始時間;max()函數(shù),找到時間序列最大值,也就是結(jié)束時間。
如何比較兩個時間點?如何計算時間跨度,或者持續(xù)時間?
In [8]: air_quality["datetime"].max() - air_quality["datetime"].min()
Out[8]: Timedelta('44 days 23:00:00')
pandas.Timestamp 可以直接計算差值,結(jié)果為pandas.Timedelta 類型,類似于python庫的時間跨度,datetime.timedelta
如何僅關(guān)注某個時間單位?
比如年,月,日,比如增加一列,表示月份?
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é)等等時間屬性。
如何計算每周,每個城市的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ù),按照周為時間單位,按城市分組然后合并歸集,計算組內(nèi)均值。
如何把時間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)前每小時的采樣頻度,更新為每個月,取最大值作為采樣值。
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ù)。
以上代碼只是一個簡單示例,示例代碼中的表達(dá)式可以根據(jù)實際問題進(jìn)行修改。
到此這篇關(guān)于詳解如何使用Pandas處理時間序列數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Pandas處理時間序列數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Sphinx環(huán)境配置及VScode編寫Rst文檔轉(zhuǎn)html的步驟
sphinx主要用于編寫 reStructuredText 和 Markdown 格式技術(shù)文檔,編寫此類技術(shù)文檔時Sphinx工具可將其轉(zhuǎn)為html、pdf、ePub等格式,這篇文章主要介紹了Sphinx環(huán)境配置及VScode編寫Rst文檔轉(zhuǎn)html,需要的朋友可以參考下2023-03-03
Python數(shù)據(jù)結(jié)構(gòu)之圖的存儲結(jié)構(gòu)詳解
本篇章主要介紹圖,包括圖的定義、相關(guān)術(shù)語、性質(zhì)及存儲結(jié)構(gòu),并用Python代碼實現(xiàn),需要的朋友可以參考下2021-06-06
Python使用itertools模塊實現(xiàn)排列組合功能示例
這篇文章主要介紹了Python使用itertools模塊實現(xiàn)排列組合功能,涉及Python基于itertools模塊product、permutations與combinations_with_replacement方法進(jìn)行排列、組合等相關(guān)操作實現(xiàn)技巧,需要的朋友可以參考下2018-07-07
Python pandas軸旋轉(zhuǎn)stack和unstack的使用說明
這篇文章主要介紹了Python pandas軸旋轉(zhuǎn)stack和unstack的使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03

