pythondatedatetimedatestamp

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


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
...

Solution

  • Adapated from Get date from week number

    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)