這篇文章主要介紹了python中Pandas庫(kù)的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)是少有的成都網(wǎng)站制作、做網(wǎng)站、營(yíng)銷型企業(yè)網(wǎng)站、微信小程序開(kāi)發(fā)、手機(jī)APP,開(kāi)發(fā)、制作、設(shè)計(jì)、賣鏈接、推廣優(yōu)化一站式服務(wù)網(wǎng)絡(luò)公司,2013年開(kāi)創(chuàng)至今,堅(jiān)持透明化,價(jià)格低,無(wú)套路經(jīng)營(yíng)理念。讓網(wǎng)頁(yè)驚喜每一位訪客多年來(lái)深受用戶好評(píng)前言
在使用Python進(jìn)行數(shù)據(jù)分析時(shí),經(jīng)常會(huì)遇到時(shí)間日期格式處理和轉(zhuǎn)換,特別是分析和挖掘與時(shí)間相關(guān)的數(shù)據(jù),比如量化交易就是從歷史數(shù)據(jù)中尋找股價(jià)的變化規(guī)律。Python中自帶的處理時(shí)間的模塊有datetime,NumPy庫(kù)也提供了相應(yīng)的方法,Pandas作為Python環(huán)境下的數(shù)據(jù)分析庫(kù),更是提供了強(qiáng)大的日期數(shù)據(jù)處理的功能,是處理時(shí)間序列的利器。
1、生成日期序列
主要提供pd.data_range()和pd.period_range()兩個(gè)方法,給定參數(shù)有起始時(shí)間、結(jié)束時(shí)間、生成時(shí)期的數(shù)目及時(shí)間頻率(freq='M'月,'D'天,‘W',周,'Y'年)等。
兩種主要區(qū)別在于pd.date_range()生成的是DatetimeIndex格式的日期序列;pd.period_range()生成的是PeriodIndex格式的日期序列。
以下通過(guò)生成月時(shí)間序列和周時(shí)間序列來(lái)對(duì)比下:
date_rng = pd.date_range('2019-01-01', freq='M', periods=12) print(f'month date_range(): {date_rng}') """ date_range(): DatetimeIndex(['2019-01-31', '2019-02-28', '2019-03-31', '2019-04-30', '2019-05-31', '2019-06-30', '2019-07-31', '2019-08-31', '2019-09-30', '2019-10-31', '2019-11-30', '2019-12-31'], dtype='datetime64[ns]', freq='M') """ period_rng = pd.period_range('2019/01/01', freq='M', periods=12) print(f'month period_range(): {period_rng}') """ period_range(): PeriodIndex(['2019-01', '2019-02', '2019-03', '2019-04', '2019-05', '2019-06', '2019-07', '2019-08', '2019-09', '2019-10', '2019-11', '2019-12'], dtype='period[M]', freq='M') """ date_rng = pd.date_range('2019-01-01', freq='W-SUN', periods=12) print(f'week date_range(): {date_rng}') """ week date_range(): DatetimeIndex(['2019-01-06', '2019-01-13', '2019-01-20', '2019-01-27', '2019-02-03', '2019-02-10', '2019-02-17', '2019-02-24', '2019-03-03', '2019-03-10', '2019-03-17', '2019-03-24'], dtype='datetime64[ns]', freq='W-SUN') """ period_rng=pd.period_range('2019-01-01',freq='W-SUN',periods=12) print(f'week period_range(): {period_rng}') """ week period_range(): PeriodIndex(['2018-12-31/2019-01-06', '2019-01-07/2019-01-13', '2019-01-14/2019-01-20', '2019-01-21/2019-01-27', '2019-01-28/2019-02-03', '2019-02-04/2019-02-10', '2019-02-11/2019-02-17', '2019-02-18/2019-02-24', '2019-02-25/2019-03-03', '2019-03-04/2019-03-10', '2019-03-11/2019-03-17', '2019-03-18/2019-03-24'], dtype='period[W-SUN]', freq='W-SUN') """ date_rng = pd.date_range('2019-01-01 00:00:00', freq='H', periods=12) print(f'hour date_range(): {date_rng}') """ hour date_range(): DatetimeIndex(['2019-01-01 00:00:00', '2019-01-01 01:00:00', '2019-01-01 02:00:00', '2019-01-01 03:00:00', '2019-01-01 04:00:00', '2019-01-01 05:00:00', '2019-01-01 06:00:00', '2019-01-01 07:00:00', '2019-01-01 08:00:00', '2019-01-01 09:00:00', '2019-01-01 10:00:00', '2019-01-01 11:00:00'], dtype='datetime64[ns]', freq='H') """ period_rng=pd.period_range('2019-01-01 00:00:00',freq='H',periods=12) print(f'hour period_range(): {period_rng}') """ hour period_range(): PeriodIndex(['2019-01-01 00:00', '2019-01-01 01:00', '2019-01-01 02:00', '2019-01-01 03:00', '2019-01-01 04:00', '2019-01-01 05:00', '2019-01-01 06:00', '2019-01-01 07:00', '2019-01-01 08:00', '2019-01-01 09:00', '2019-01-01 10:00', '2019-01-01 11:00'], dtype='period[H]', freq='H') """
2、生成Timestamp對(duì)象及轉(zhuǎn)換
創(chuàng)建一個(gè)Timestamp時(shí)間戳對(duì)象有pd.Timestamp()方法和pd.to_datetime()方法。如下所示:
ts=pd.Timestamp(2019,1,1) print(f'pd.Timestamp()-1:{ts}') #pd.Timestamp()-1:2019-01-01 00:00:00 ts=pd.Timestamp(dt(2019,1,1,hour=0,minute=1,second=1)) print(f'pd.Timestamp()-2:{ts}') #pd.Timestamp()-2:2019-01-01 00:01:01 ts=pd.Timestamp("2019-1-1 0:1:1") print(f'pd.Timestamp()-3:{ts}') #pd.Timestamp()-3:2019-01-01 00:01:01 print(f'pd.Timestamp()-type:{type(ts)}') #pd.Timestamp()-type:<class 'pandas._libs.tslibs.timestamps.Timestamp'> #dt=pd.to_datetime(2019,1,1) 不支持 dt=pd.to_datetime(dt(2019,1,1,hour=0,minute=1,second=1)) print(f'pd.to_datetime()-1:{dt}') #pd.to_datetime()-1:2019-01-01 00:01:01 dt=pd.to_datetime("2019-1-1 0:1:1") print(f'pd.to_datetime()-2:{dt}') #pd.to_datetime()-2:2019-01-01 00:01:01 print(f'pd.to_datetime()-type:{type(dt)}') #pd.to_datetime()-type:<class 'pandas._libs.tslibs.timestamps.Timestamp'> #pd.to_datetime生成自定義時(shí)間序列 dtlist=pd.to_datetime(["2019-1-1 0:1:1", "2019-3-1 0:1:1"]) print(f'pd.to_datetime()-list:{dtlist}') #pd.to_datetime()-list:DatetimeIndex(['2019-01-01 00:01:01', '2019-03-01 00:01:01'], dtype='datetime64[ns]', freq=None) #時(shí)間戳轉(zhuǎn)換為period月時(shí)期 pr = ts.to_period('M') print(f'ts.to_period():{pr}') #ts.to_period():2019-01 print(f'pd.to_period()-type:{type(pr)}') #pd.to_period()-type:<class 'pandas._libs.tslibs.period.Period'>
3、生成period對(duì)象及轉(zhuǎn)換
#定義時(shí)期period per=pd.Period('2019') print(f'pd.Period():{per}') #pd.Period():2019 per_del=pd.Period('2019')-pd.Period('2018') print(f'2019和2018間隔{per_del}年')#可以直接+、-整數(shù)(代表年) #2019和2018間隔1年 #時(shí)期轉(zhuǎn)換為時(shí)間戳 print(per.to_timestamp(how='end'))#2019-12-31 00:00:00 print(per.to_timestamp(how='start'))#2019-01-01 00:00:00
4、生成時(shí)間間隔Timedelta
#生成時(shí)間間隔Timedelta print(pd.Timedelta(days=5, minutes=50, seconds=20, milliseconds=10, microseconds=10, nanoseconds=10)) #5 days 00:50:20.010010 #獲取當(dāng)前時(shí)間 now=pd.datetime.now() #計(jì)算當(dāng)前時(shí)間往后50天的日期 dt=now+pd.Timedelta(days=50) print(f'當(dāng)前時(shí)間是{now}, 50天后時(shí)間是{dt}') #當(dāng)前時(shí)間是2019-06-08 17:59:31.726065, 50天后時(shí)間是2019-07-28 17:59:31.726065 #只顯示年月日 print(dt.strftime('%Y-%m-%d'))#2019-07-28
5、重采樣及頻率轉(zhuǎn)換
#asfreq 按季度顯示索引值 #'DatetimeIndex' object has no attribute 'asfreq' date=pd.date_range('1/1/2018', periods=20, freq='D') tsdat_series=pd.Series(range(20),index=date) tsp_series=tsdat_series.to_period('D') print(tsp_series.index.asfreq('Q')) date=pd.period_range('1/1/2018', periods=20, freq='D') tsper_series=pd.Series(range(20),index=date) print(tsper_series.index.asfreq('Q')) """ PeriodIndex(['2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1', '2018Q1'], dtype='period[Q-DEC]', freq='Q-DEC') """ #resample 按季度統(tǒng)計(jì)并顯示 print(tsdat_series.resample('Q').sum().to_period('Q')) """ 2018Q1 190 Freq: Q-DEC, dtype: int64 """ #groupby 按周進(jìn)行匯總求平均值 print(tsdat_series.groupby(lambda x:x.weekday).mean()) """ 0 7.0 1 8.0 2 9.0 3 10.0 4 11.0 5 12.0 6 9.5 dtype: float64 """
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“python中Pandas庫(kù)的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
文章題目:python中Pandas庫(kù)的示例分析-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://muchs.cn/article2/idjic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、虛擬主機(jī)、自適應(yīng)網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)、企業(yè)建站、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容