當(dāng)數(shù)據(jù)很多,且日期格式不標(biāo)準(zhǔn)時的時候,如果pandas.to_datetime 函數(shù)使用不當(dāng),會使得處理時間變得很長,提升速度的關(guān)鍵在于format的使用。下面舉例進(jìn)行說明:
示例數(shù)據(jù):
date 格式:02.01.2013 即 日.月.年
數(shù)據(jù)量:3000000
transcation.head()--------------------------------------------- date date_block_num shop_id item_id item_price item_cnt_day0 02.01.2013 0 59 22154 999.00 1.01 03.01.2013 0 25 2552 899.00 1.02 05.01.2013 0 25 2552 899.00 -1.03 06.01.2013 0 25 2554 1709.05 1.04 15.01.2013 0 25 2555 1099.00 1.0
處理方式一:
transactions['date_formatted']=pd.to_datetime(transactions['date'])
處理時間: 10min
處理方式二:
transactions['date_formatted']=pd.to_datetime(transactions['date'], format='%d.%m.%Y')
處理時間:10s
附錄:format相關(guān)
| 代碼 | 說明 |
|---|---|
| %Y | 4位數(shù)的年 |
| %y | 2位數(shù)的年 |
| %m | 2位數(shù)的月[01,12] |
| %d | 2位數(shù)的日[01,31] |
| %H | 時(24小時制)[00,23] |
| %l | 時(12小時制)[01,12] |
| %M | 2位數(shù)的分[00,59] |
| %S | 秒[00,61]有閏秒的存在 |
| %w | 用整數(shù)表示的星期幾[0(星期天),6] |
| %F | %Y-%m-%d簡寫形式例如,2017-06-27 |
| %D | %m/%d/%y簡寫形式 |
以上這篇pandas 快速處理 date_time 日期格式方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答