程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python strftime behavior and examples of converting to date format

編輯:Python
# Convert to date format or specify date format , Be careful format The specified date format is the acquired date format
>>> ebola['date_dt']=pd.to_datetime(ebola['Date'],format='%m/%d/%Y')
>>> print(ebola.iloc[:5,:5])
Date Day Cases_Guinea Cases_Liberia Cases_SierraLeone
0 1/5/2015 289 2776.0 NaN 10030.0
1 1/4/2015 288 2775.0 NaN 9780.0
2 1/3/2015 287 2769.0 8166.0 9722.0
3 1/2/2015 286 NaN 8157.0 NaN
4 12/31/2014 284 2730.0 8115.0 9633.0
>>> ebola['date_dt']=pd.to_datetime(ebola['Date'],format='%d/%m/%Y')
# Due to the output above ebola The data shows that , The fifth row of data has 31, It cannot be used as a month , Therefore, the following error occurs
ValueError: time data '12/31/2014' does not match format '%d/%m/%Y' (match)

The character meaning specified when converting to date format

Symbol

meaning

%a Abbreviated name of the week %A The full name of the week %w Show weeks in numbers ,0 It's Sunday %d Every day of the month ( Two figures )%b The abbreviation of the month name %B The full form of the month name %m month ( Two digits )%y year ( Two digits )%Y year ( Four digit number )%H Hours ( Two digit number ,24 hourly )%I Hours ( Two digit number ,12 hourly )%pAM or PM%M minute ( Two digit number )%S second ( Two digit number )%f Microsecond %zUTC The offset ( +HHMM or -HHMM )%Z Time zone name %j Every day of the year ( Three digits )%U Weeks of the year ( Sunday is the first day )%W Weeks of the year ( Monday is the first day )%c Date and time represent %x The date means %X Time means                                             %%% character %GISO 8601 year %uISO 8601 week %VISO 8601 week
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved