亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python+pandas生成指定日期和重采樣的方法

 更新時(shí)間:2018年04月11日 10:11:23   作者:LY_ysys629  
下面小編就為大家分享一篇python+pandas生成指定日期和重采樣的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧

python 日期的范圍、頻率、重采樣以及頻率轉(zhuǎn)換

pandas有一整套的標(biāo)準(zhǔn)時(shí)間序列頻率以及用于重采樣、頻率推斷、生成固定頻率日期范圍的工具。

生成指定日期范圍的范圍

pandas.date_range()用于生成指定長(zhǎng)度的DatatimeIndex:

1)默認(rèn)情況下,date_range會(huì)按著時(shí)間間隔為天的方式生成從給定開始到結(jié)束時(shí)間的時(shí)間戳數(shù)組;

2)如果只指定開始或結(jié)束時(shí)間,還需要periods標(biāo)定時(shí)間長(zhǎng)度。

import pandas as pd
pd.date_range('2017-6-20','2017-6-27')
DatetimeIndex(['2017-06-20', '2017-06-21', '2017-06-22', '2017-06-23',
   '2017-06-24', '2017-06-25', '2017-06-26', '2017-06-27'],
   dtype='datetime64[ns]', freq='D')
pd.date_range('2017-6-20 12:59:30','2017-6-27')
DatetimeIndex(['2017-06-20 12:59:30', '2017-06-21 12:59:30',
   '2017-06-22 12:59:30', '2017-06-23 12:59:30',
   '2017-06-24 12:59:30', '2017-06-25 12:59:30',
   '2017-06-26 12:59:30'],
   dtype='datetime64[ns]', freq='D')
pd.date_range('2017-6-20 12:59:30',periods = 8)
DatetimeIndex(['2017-06-20 12:59:30', '2017-06-21 12:59:30',
   '2017-06-22 12:59:30', '2017-06-23 12:59:30',
   '2017-06-24 12:59:30', '2017-06-25 12:59:30',
   '2017-06-26 12:59:30', '2017-06-27 12:59:30'],
   dtype='datetime64[ns]', freq='D')
pd.date_range('2017-6-20 12:59:30',periods = 8, normalize = True)
 DatetimeIndex(['2017-06-20', '2017-06-21', '2017-06-22', '2017-06-23',
   '2017-06-24', '2017-06-25', '2017-06-26', '2017-06-27'],
   dtype='datetime64[ns]', freq='D')

頻率和日期偏移量

pandas中的頻率是由一個(gè)基礎(chǔ)頻率(M、H)也可以是(Hour、Minute、h、min等)

pd.date_range('2017-6-27',periods = 7,freq = '1h30min')
DatetimeIndex(['2017-06-27 00:00:00', '2017-06-27 01:30:00',
   '2017-06-27 03:00:00', '2017-06-27 04:30:00',
   '2017-06-27 06:00:00', '2017-06-27 07:30:00',
   '2017-06-27 09:00:00'],
   dtype='datetime64[ns]', freq='90T')
pd.date_range('2017-6-27',periods = 7,freq = 'M')
DatetimeIndex(['2017-06-30', '2017-07-31', '2017-08-31', '2017-09-30',
   '2017-10-31', '2017-11-30', '2017-12-31'],
   dtype='datetime64[ns]', freq='M')
pd.date_range('2017-6-27',periods = 7,freq = 'd')
 DatetimeIndex(['2017-06-27', '2017-06-28', '2017-06-29', '2017-06-30',
   '2017-07-01', '2017-07-02', '2017-07-03'],
   dtype='datetime64[ns]', freq='D')
pd.date_range('2017-6-27',periods = 7,freq = 'H')
 DatetimeIndex(['2017-06-27 00:00:00', '2017-06-27 01:00:00',
   '2017-06-27 02:00:00', '2017-06-27 03:00:00',
   '2017-06-27 04:00:00', '2017-06-27 05:00:00',
   '2017-06-27 06:00:00'],
   dtype='datetime64[ns]', freq='H')

常用的基礎(chǔ)頻率

別名 偏移量 說(shuō)明
D/d Day 每日歷日
B BusinessDay 每工作日
H/h Hour 每小時(shí)
T或min Minute 每分
S Secend 每秒
L或ms Milli 每毫秒(每千分之一秒)
U Micro 每微秒(即百萬(wàn)分之一秒)
M MonthEnd 每月最后一個(gè)日歷日
BM BusinessDayEnd 每月最后一個(gè)工作

上表只展示了部分!

WOM日期(可獲得例如“每月第3個(gè)星期五”)

pd.date_range('2017-06-01','2017-07-31',freq='WOM-3FRI')
DatetimeIndex(['2017-06-16', '2017-07-21'], dtype='datetime64[ns]', freq='WOM-3FRI')

重采樣及頻率轉(zhuǎn)換

降采樣:高頻數(shù)據(jù)到低頻數(shù)據(jù)

升采樣:低頻數(shù)據(jù)到高頻數(shù)據(jù)

主要函數(shù):resample()(pandas對(duì)象都會(huì)有這個(gè)方法)

resample方法的參數(shù)

參數(shù) 說(shuō)明
freq 表示重采樣頻率,例如‘M'、‘5min',Second(15)
how='mean' 用于產(chǎn)生聚合值的函數(shù)名或數(shù)組函數(shù),例如‘mean'、‘ohlc'、np.max等,默認(rèn)是‘mean',其他常用的值由:‘first'、‘last'、‘median'、‘max'、‘min'
axis=0 默認(rèn)是縱軸,橫軸設(shè)置axis=1
fill_method = None 升采樣時(shí)如何插值,比如‘ffill'、‘bfill'等
closed = ‘right' 在降采樣時(shí),各時(shí)間段的哪一段是閉合的,‘right'或‘left',默認(rèn)‘right'
label= ‘right' 在降采樣時(shí),如何設(shè)置聚合值的標(biāo)簽,例如,9:30-9:35會(huì)被標(biāo)記成9:30還是9:35,默認(rèn)9:35
loffset = None 面元標(biāo)簽的時(shí)間校正值,比如‘-1s'或Second(-1)用于將聚合標(biāo)簽調(diào)早1秒
limit=None 在向前或向后填充時(shí),允許填充的最大時(shí)期數(shù)
kind = None 聚合到時(shí)期(‘period')或時(shí)間戳(‘timestamp'),默認(rèn)聚合到時(shí)間序列的索引類型
convention = None 當(dāng)重采樣時(shí)期時(shí),將低頻率轉(zhuǎn)換到高頻率所采用的約定(start或end)。默認(rèn)‘end'

降采樣

需考慮:

1)各區(qū)間哪邊是閉合的(參數(shù):closed)

2)如何標(biāo)記各聚合面元,用區(qū)間的開頭還是末尾(參數(shù):label)

ts_index = pd.date_range('2017-06-20',periods =12,freq = '1min')#一分鐘采樣數(shù)據(jù)
ts = pd.Series(np.arange(12),index = ts_index)
ts
 2017-06-20 00:00:00 0
 2017-06-20 00:01:00 1
 2017-06-20 00:02:00 2
 2017-06-20 00:03:00 3
 2017-06-20 00:04:00 4
 2017-06-20 00:05:00 5
 2017-06-20 00:06:00 6
 2017-06-20 00:07:00 7
 2017-06-20 00:08:00 8
 2017-06-20 00:09:00 9
 2017-06-20 00:10:00 10
 2017-06-20 00:11:00 11
 Freq: T, dtype: int32

聚合到5分鐘

ts.resample('5min',how='sum')
C:\Program Files\anaconda\lib\site-packages\ipykernel\__main__.py:1: FutureWarning: how in .resample() is deprecated
 the new syntax is .resample(...).sum()
 if __name__ == '__main__':
 2017-06-20 00:00:00 10
 2017-06-20 00:05:00 35
 2017-06-20 00:10:00 21
 Freq: 5T, dtype: int32
ts.resample('5min',how='sum',closed='left')
C:\Program Files\anaconda\lib\site-packages\ipykernel\__main__.py:1: FutureWarning: how in .resample() is deprecated
 the new syntax is .resample(...).sum()
 if __name__ == '__main__':
 2017-06-20 00:00:00 10
 2017-06-20 00:05:00 35
 2017-06-20 00:10:00 21
 Freq: 5T, dtype: int32
ts.resample('5min',how='sum',closed='left',label ='left')
 C:\Program Files\anaconda\lib\site-packages\ipykernel\__main__.py:1: FutureWarning: how in .resample() is deprecated
 the new syntax is .resample(...).sum()
 if __name__ == '__main__':
 2017-06-20 00:00:00 10
 2017-06-20 00:05:00 35
 2017-06-20 00:10:00 21
 Freq: 5T, dtype: int32

通過groupby進(jìn)行重插樣

另外一種降采樣方法

ts1_index = pd.date_range('2017-6-01',periods = 100,freq = 'd')
ts1 = pd.Series(np.arange(100),index = ts1_index)
ts1.head()
2017-06-01 0
 2017-06-02 1
 2017-06-03 2
 2017-06-04 3
 2017-06-05 4
 Freq: D, dtype: int32
ts1.groupby(lambda x:x.month).mean()
 6 14.5
 7 45.0
 8 76.0
 9 95.5
 dtype: float64
ts1.groupby(lambda x:x.weekday).mean()
 
 0 49.5
 1 50.5
 2 51.5
 3 49.0
 4 50.0
 5 47.5
 6 48.5
 dtype: float64
df1 = pd.DataFrame(np.arange(200).reshape(100,2),index = ts1_index)
df1.groupby(lambda x:x.weekday).mean()

0 1
0 99 100
1 101 102
2 103 104
3 98 99
4 100 101
5 95 96
6 97 98

對(duì)于具有時(shí)間序列索引的pandas數(shù)據(jù)結(jié)構(gòu),當(dāng)groupby傳入一個(gè)函數(shù)時(shí),可以對(duì)時(shí)間索引對(duì)應(yīng)列進(jìn)行聚合

升采樣

升采樣沒有聚合,但是需要填充

df2 = pd.DataFrame(np.arange(200).reshape(100,2),index = ts1_index,columns=['add1','add2'])
df2.head()

add1 add2
2017-06-01 0 1
2017-06-02 2 3
2017-06-03 4 5
2017-06-04 6 7
2017-06-05 8 9

df2.resample('W-THU',fill_method = 'ffill')
 C:\Program Files\anaconda\lib\site-packages\ipykernel\__main__.py:1: FutureWarning: fill_method is deprecated to .resample()
 the new syntax is .resample(...).ffill()
 if __name__ == '__main__':

add1 add2
2017-06-01 0 1
2017-06-08 14 15
2017-06-15 28 29
2017-06-22 42 43
2017-06-29 56 57
2017-07-06 70 71
2017-07-13 84 85
2017-07-20 98 99
2017-07-27 112 113
2017-08-03 126 127
2017-08-10 140 141
2017-08-17 154 155
2017-08-24 168 169
2017-08-31 182 183
2017-09-07 196 197
2017-09-14 198 199

總結(jié)

本篇博客主要內(nèi)容:

1)生成指定時(shí)間段,指定頻率的日期

2)對(duì)含有時(shí)間索引的pandas數(shù)據(jù)進(jìn)行重采樣,包括降采樣和升采樣等。

相關(guān)文章

  • Python使用Virtualenv進(jìn)行虛擬環(huán)境管理的詳細(xì)步驟

    Python使用Virtualenv進(jìn)行虛擬環(huán)境管理的詳細(xì)步驟

    Virtualenv是一個(gè)Python環(huán)境管理工具,它允許開發(fā)者在不同的項(xiàng)目之間獨(dú)立創(chuàng)建和管理各自的Python環(huán)境,通過virtualenv,你可以為每個(gè)項(xiàng)目安裝特定版本的Python解釋器以及項(xiàng)目的依賴庫(kù),本文給大家介紹了Python使用Virtualenv進(jìn)行虛擬環(huán)境管理的詳細(xì)步驟
    2024-09-09
  • 詳解python while 函數(shù)及while和for的區(qū)別

    詳解python while 函數(shù)及while和for的區(qū)別

    這篇文章主要介紹了python while 函數(shù)及while和for的區(qū)別 ,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • 一文帶你了解Python中不同數(shù)據(jù)對(duì)象的空值校驗(yàn)方法

    一文帶你了解Python中不同數(shù)據(jù)對(duì)象的空值校驗(yàn)方法

    空值校驗(yàn)在數(shù)據(jù)處理和應(yīng)用程序開發(fā)中是一個(gè)非常重要的任務(wù),Python提供了多種方式來(lái)檢查不同數(shù)據(jù)對(duì)象(如字符串、列表、字典、集合等)是否為空或包含空值,下面就跟隨小編一起來(lái)學(xué)習(xí)一下吧
    2024-01-01
  • 使用Keras 實(shí)現(xiàn)查看model weights .h5 文件的內(nèi)容

    使用Keras 實(shí)現(xiàn)查看model weights .h5 文件的內(nèi)容

    這篇文章主要介紹了使用Keras 實(shí)現(xiàn)查看model weights .h5 文件的內(nèi)容,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-06-06
  • Python對(duì)Excel進(jìn)行處理的實(shí)操指南

    Python對(duì)Excel進(jìn)行處理的實(shí)操指南

    這篇文章主要給大家介紹了關(guān)于Python對(duì)Excel進(jìn)行處理的實(shí)操指南,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Python3讀取文件常用方法實(shí)例分析

    Python3讀取文件常用方法實(shí)例分析

    這篇文章主要介紹了Python3讀取文件常用方法,以實(shí)例形式較為詳細(xì)的分析了Python一次性讀取、逐行讀取及讀取文件一部分的實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-05-05
  • python?spotlight庫(kù)簡(jiǎn)化交互式方法探索數(shù)據(jù)分析

    python?spotlight庫(kù)簡(jiǎn)化交互式方法探索數(shù)據(jù)分析

    這篇文章主要為大家介紹了python?spotlight庫(kù)簡(jiǎn)化的交互式方法探索數(shù)據(jù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • 利用Python破解摩斯密碼

    利用Python破解摩斯密碼

    摩爾斯電碼( 又譯為摩斯密碼,英語(yǔ):Morse code)是一種時(shí)通時(shí)斷的信號(hào)代碼,通過不同的排列順序來(lái)表達(dá)不同的英文字母、數(shù)字和標(biāo)點(diǎn)符號(hào)。本文將通過Python代碼來(lái)實(shí)現(xiàn)破解摩斯密碼,感興趣的可以學(xué)習(xí)一下
    2022-02-02
  • pytorch 兩個(gè)GPU同時(shí)訓(xùn)練的解決方案

    pytorch 兩個(gè)GPU同時(shí)訓(xùn)練的解決方案

    這篇文章主要介紹了pytorch 兩個(gè)GPU同時(shí)訓(xùn)練的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Python實(shí)現(xiàn)自動(dòng)發(fā)消息自定義內(nèi)容的操作代碼

    Python實(shí)現(xiàn)自動(dòng)發(fā)消息自定義內(nèi)容的操作代碼

    這篇文章主要介紹了Python實(shí)現(xiàn)自動(dòng)發(fā)消息自定義內(nèi)容的操作代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08

最新評(píng)論