Pandas-如何輕松處理時(shí)間序列數(shù)據(jù)
時(shí)間序列數(shù)據(jù)在數(shù)據(jù)分析建模中很常見,例如天氣預(yù)報(bào),空氣狀態(tài)監(jiān)測(cè),股票交易等金融場(chǎng)景。此處選擇巴黎、倫敦歐洲城市空氣質(zhì)量監(jiān)測(cè) N O 2 NO_2 NO2?數(shù)據(jù)作為樣例。
python數(shù)據(jù)分析-數(shù)據(jù)表讀寫到pandas
經(jīng)典算法-遺傳算法的python實(shí)現(xiàn)
經(jīng)典算法-遺傳算法的一個(gè)簡(jiǎn)單例子
大模型查詢工具助手之股票免費(fèi)查詢接口
Falcon構(gòu)建輕量級(jí)的REST API服務(wù)
決策引擎-利用Drools實(shí)現(xiàn)簡(jiǎn)單防火墻策略
Python技巧-終端屏幕打印光標(biāo)和文字控制
監(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è)簡(jiǎn)單示例,示例代碼中的表達(dá)式可以根據(jù)實(shí)際問題進(jìn)行修改。
覺得有用 收藏 收藏 收藏
點(diǎn)個(gè)贊 點(diǎn)個(gè)贊 點(diǎn)個(gè)贊
End
GPT專欄文章:
GPT實(shí)戰(zhàn)系列-ChatGLM3本地部署CUDA11+1080Ti+顯卡24G實(shí)戰(zhàn)方案
GPT實(shí)戰(zhàn)系列-LangChain + ChatGLM3構(gòu)建天氣查詢助手
大模型查詢工具助手之股票免費(fèi)查詢接口
GPT實(shí)戰(zhàn)系列-簡(jiǎn)單聊聊LangChain
GPT實(shí)戰(zhàn)系列-大模型為我所用之借用ChatGLM3構(gòu)建查詢助手
GPT實(shí)戰(zhàn)系列-P-Tuning本地化訓(xùn)練ChatGLM2等LLM模型,到底做了什么?(二)
GPT實(shí)戰(zhàn)系列-P-Tuning本地化訓(xùn)練ChatGLM2等LLM模型,到底做了什么?(一)
GPT實(shí)戰(zhàn)系列-ChatGLM2模型的微調(diào)訓(xùn)練參數(shù)解讀
GPT實(shí)戰(zhàn)系列-如何用自己數(shù)據(jù)微調(diào)ChatGLM2模型訓(xùn)練
GPT實(shí)戰(zhàn)系列-ChatGLM2部署Ubuntu+Cuda11+顯存24G實(shí)戰(zhàn)方案
GPT實(shí)戰(zhàn)系列-Baichuan2本地化部署實(shí)戰(zhàn)方案
GPT實(shí)戰(zhàn)系列-Baichuan2等大模型的計(jì)算精度與量化
GPT實(shí)戰(zhàn)系列-GPT訓(xùn)練的Pretraining,SFT,Reward Modeling,RLHF 文章來源:http://www.zghlxwxcb.cn/news/detail-822502.html
GPT實(shí)戰(zhàn)系列-探究GPT等大模型的文本生成-CSDN博客文章來源地址http://www.zghlxwxcb.cn/news/detail-822502.html
到了這里,關(guān)于數(shù)據(jù)分析-Pandas如何輕松處理時(shí)間序列數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!