-2

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
...
azro
  • 53,056
  • 7
  • 34
  • 70
InVinci
  • 71
  • 8

1 Answers1

1

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)
azro
  • 53,056
  • 7
  • 34
  • 70
  • It's a long series of dates. The DD that I have shown in the example is relevant to wrt to the day. Could you please show a scalable solution? – InVinci Dec 07 '21 at 18:40
  • @InVinci what do you mean ? you just have to pass your format here – azro Dec 07 '21 at 18:42
  • @azro Your solution throws this error -- TypeError: strptime() argument 1 must be str, not Series – InVinci Dec 07 '21 at 18:45
  • @InVinci You may need to learn how to apply a method on a Series, I'll update my code – azro Dec 07 '21 at 18:49