python pandas時序處理相關(guān)功能詳解
創(chuàng)建時間序列
函數(shù)pd.date_range()
根據(jù)指定的范圍,生成時間序列DatetimeIndex,每隔元素的類型為Timestamp。該函數(shù)應(yīng)用較多。
ts = pd.date_range('2017-09-01', periods=10, freq='d', normalize=False)
ts
輸出為:
DatetimeIndex(['2017-09-01', '2017-09-02', '2017-09-03', '2017-09-04', '2017-09-05', '2017-09-06', '2017-09-07', '2017-09-08', '2017-09-09', '2017-09-10'], dtype='datetime64[ns]', freq='D'
主要的入?yún)⒔馕觯?br />
- start: 開始時刻,可以是字符串或者datetime類型的值。默認(rèn)None。
- end: 結(jié)束時刻,可以是字符串或者datetime類型的值,如果指定了長度,即periods,則可不設(shè)置。默認(rèn)None。
- periods: 時序的長度,整型類型。如果有end,可不設(shè)置。默認(rèn)None。
- freq: 時序生成的頻率,即每隔多少時刻生成一個時序點(diǎn)。字符串類型或者DateOffset類型。默認(rèn)'D',即天粒度,見上述代碼輸出。
- tz: 時區(qū),字符串類型。默認(rèn)None。
- normalize: bool類型,沒用過,不知道干啥的。
- name: 設(shè)置時序的名稱,字符串類型,默認(rèn)None。
- closed: 是否包含兩邊的值。默認(rèn)None,即兩邊都保留。
其中,freq的取值可以為如下的符號表示間隔,可以結(jié)合符號和數(shù)字,如'3d',表示每隔三天記錄一個時間點(diǎn)。大小寫都可以。
B business day frequency C custom business day frequency (experimental) D calendar day frequency W weekly frequency M month end frequency SM semi-month end frequency (15th and end of month) BM business month end frequency CBM custom business month end frequency MS month start frequency SMS semi-month start frequency (1st and 15th) BMS business month start frequency CBMS custom business month start frequency Q quarter end frequency BQ business quarter endfrequency QS quarter start frequency BQS business quarter start frequency A year end frequency BA business year end frequency AS year start frequency BAS business year start frequency BH business hour frequency H hourly frequency T, min minutely frequency S secondly frequency L, ms milliseconds U, us microseconds N nanoseconds
字符串轉(zhuǎn)換為時間戳
pd.to_datetime() 函數(shù)可以將表示時間的字符串轉(zhuǎn)換位TimeStamp。
pd.to_datetime('2017-09-01')
輸出為:
Timestamp('2017-09-01 00:00:00')
常用的參數(shù):
format: 用來設(shè)置字符串的格式,默認(rèn)如上所示。
時間戳的加減
有時候需要將時間進(jìn)行增減,可以使用類型:DateOffset。
pd.to_datetime('2017-09-01') + pd.DateOffset(days=10)
輸出為:
Timestamp('2017-09-11 00:00:00')
DateOffset常用的參數(shù):
- months,設(shè)置月。
- days,設(shè)置天。
- years,設(shè)置年。
- hours,設(shè)置小時。
- minutes,設(shè)置分鐘。
- seconds,設(shè)置秒。
以上可以同時設(shè)置,組合使用。
pd.to_datetime('2017-09-01') + pd.DateOffset(seconds=10, days = 10)
輸出為:
Timestamp('2017-09-11 00:00:10')
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python matplotlib實(shí)現(xiàn)雙Y軸的實(shí)例
今天小編就為大家分享一篇python matplotlib實(shí)現(xiàn)雙Y軸的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02
Django1.7+python 2.78+pycharm配置mysql數(shù)據(jù)庫教程
原本感覺在Django1.7+python 2.78+pycharm環(huán)境下配置mysql數(shù)據(jù)庫是件很容易的事情,結(jié)果具體操作的時候才發(fā)現(xiàn),問題還是挺多的,這里記錄一下最終的配置結(jié)果,給需要的小伙伴參考下吧2014-11-11
python 如何利用argparse解析命令行參數(shù)
這篇文章主要介紹了python 利用argparse解析命令行參數(shù)的步驟,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-09-09
僅用50行Python代碼實(shí)現(xiàn)一個簡單的代理服務(wù)器
這篇文章主要介紹了僅用50行Python代碼實(shí)現(xiàn)一個簡單的代理服務(wù)器,利用最簡單的client->proxy->forward原理在socket模塊下編寫,需要的朋友可以參考下2015-04-04
python實(shí)現(xiàn)決策樹ID3算法的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)決策樹ID3算法的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05

