Convet W(Number)-Y(NUMBER) to date in Python

34 Views Asked by At

Using Python:

How do we convert a series of dates W(Number)-Y(NUMBER) in the following format 'YYYY-MM-DD'

Example:

W01-16
W02-16
W03-16
...

To:

2016-01-04
2016-01-11
2016-01-18
...
1

There are 1 best solutions below

4
azro On BEST ANSWER

Adapated from Get date from week number

  • %w for day of week, giving 1 to be monday
  • %W for week of year
  • %y for year on 2 digits
from datetime import datetime

d = "W01-16"
print(datetime.strptime('1-' + d, "%w-W%W-%y"))  # 2016-01-04 00:00:00

from datetime import datetime
from pandas import Series

s = Series(["W01-16", "W02-16", "W03-16"])
s = s.apply(lambda d: datetime.strptime('1-' + d, "%w-W%W-%y"))
print(s)