日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python pandas時序處理相關功能詳解

系統 1703 0

創建時間序列

函數pd.date_range()

根據指定的范圍,生成時間序列DatetimeIndex,每隔元素的類型為Timestamp。該函數應用較多。

            
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'
          

主要的入參解析:

  • start: 開始時刻,可以是字符串或者datetime類型的值。默認None。
  • end: 結束時刻,可以是字符串或者datetime類型的值,如果指定了長度,即periods,則可不設置。默認None。
  • periods: 時序的長度,整型類型。如果有end,可不設置。默認None。
  • freq: 時序生成的頻率,即每隔多少時刻生成一個時序點。字符串類型或者DateOffset類型。默認'D',即天粒度,見上述代碼輸出。
  • tz: 時區,字符串類型。默認None。
  • normalize: bool類型,沒用過,不知道干啥的。
  • name: 設置時序的名稱,字符串類型,默認None。
  • closed: 是否包含兩邊的值。默認None,即兩邊都保留。

其中,freq的取值可以為如下的符號表示間隔,可以結合符號和數字,如'3d',表示每隔三天記錄一個時間點。大小寫都可以。

            
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
          

字符串轉換為時間戳

pd.to_datetime() 函數可以將表示時間的字符串轉換位TimeStamp。

            
pd.to_datetime('2017-09-01')
          

輸出為:

            
Timestamp('2017-09-01 00:00:00')
          

常用的參數:

format : 用來設置字符串的格式,默認如上所示。

時間戳的加減
有時候需要將時間進行增減,可以使用類型:DateOffset。

            
pd.to_datetime('2017-09-01') + pd.DateOffset(days=10) 
          

輸出為:

            
Timestamp('2017-09-11 00:00:00')
          

DateOffset常用的參數:

  • months ,設置月。
  • days ,設置天。
  • years ,設置年。
  • hours ,設置小時。
  • minutes ,設置分鐘。
  • seconds ,設置秒。

以上可以同時設置,組合使用。

            
pd.to_datetime('2017-09-01') + pd.DateOffset(seconds=10, days = 10)
          

輸出為:

            
Timestamp('2017-09-11 00:00:10')
          

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!??!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 绍兴县| 弥渡县| 东阿县| 高邮市| 兴海县| 商都县| 浠水县| 呼玛县| 临西县| 广东省| 东乌珠穆沁旗| 平遥县| 楚雄市| 确山县| 兴化市| 赤壁市| 且末县| 衡东县| 玛多县| 丰原市| 简阳市| 外汇| 淮南市| 武宣县| 东乌| 维西| 新绛县| 和林格尔县| 大名县| 深州市| 肃宁县| 米泉市| 志丹县| 宜君县| 女性| 泌阳县| SHOW| 驻马店市| 商河县| 马公市| 临清市|