Yogesh Govindan
Yogesh Govindan

Reputation: 381

How to convert Datetime ("2020-09-27 07:14:01.114051200") to week number?

I want to find the week number of a date column in my datasets. The date is in the format "2020-09-27 07:14:01.114051200". However I could not convert to week number.

I tried the below code:

date=mydate.strftime("%W")

I'm getting this error: AttributeError: 'Series' object has no attribute 'strftime'

Upvotes: 1

Views: 457

Answers (1)

CryptoFool
CryptoFool

Reputation: 23099

You need to first convert your date in string form to a datetime object. Then you can ask for the week number from that object.

import datetime
datestr = "2020-09-27 07:14:01.114051200"
mydate = datetime.datetime.strptime(datestr.split('.')[0] , '%Y-%m-%d %H:%M:%S')
date=mydate.strftime("%W")
print(date)

Result:

38

Upvotes: 4

Related Questions