YYYY-WNN format to "%d/%m/%Y" date format

Viewed 30

Hi i have a pandas columns named YearWeekISO that has the YYYY- WNN date format, for instance 2020-W53. I want to convert that whole column to a date time format of day-month-year.

This is the code i am using: pd.to_datetime(df['YearWeekISO'], format="%Y-W%U").dt.strftime("%d/%m/%Y")

This is the error output: Cannot use '%W' or '%U' without day and year

Thanks,best regards

1 Answers

Set up the day of week:

df['date'] = pd.to_datetime(df['YearWeekISO']+'0', format='%Y-W%U%w').dt.strftime("%d/%m/%Y")

Output:

  YearWeekISO       date
0    2020-W53 03/01/2021
Related