简体   繁体   中英

How to convert date to week number that follow the year?

I have the following code:

df=pd.DataFrame(df['Created'])
Created=pd.DatetimeIndex(df['Created'])
df['CreatedDate']=Created.date
df['CreatedTime']=Created.time
df['CreatedDate']=pd.to_datetime(df['CreatedDate'], errors='coerce')
df['CreatedWW']=df['CreatedDate'].dt.week

Output: 在此处输入图片说明

I wish to get output which is for example:

CreatedWW
201715
201713
201713
.
.
201646

Anyone can share me idea?

You need to_datetime with parameter format and then strftime :

All formats for both functions are in http://strftime.org/

df = pd.DataFrame({'Created':['12/Apr/17 1:21 PM','27/Mar/17 11:06 AM']})
print (df)
              Created
0   12/Apr/17 1:21 PM
1  27/Mar/17 11:06 AM

df['new'] = pd.to_datetime(df['Created'], format='%d/%b/%y %H:%M %p').dt.strftime('%Y%V')
print (df)
              Created     new
0   12/Apr/17 1:21 PM  201715
1  27/Mar/17 11:06 AM  201713

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM