Home > database >  Parse Year Week columns to Date
Parse Year Week columns to Date

Time:05-14

I have a data frame with columns Year and Week that I am trying to parse to date into a new column called Date.

import datetime
df['Date']=datetime.datetime.fromisocalendar(df['Year'], df['Week'], 1)
 

But this generates the following error: 'cannot convert the series to <class 'int'>'.

My desired outcome is to give the Sunday Date of each week.

For example:

Year: 2022

Week: 01

Expected Date: 2022-01-02

I know there are similar posts to this already, and I have tried to manipulate, but I was unsuccessful.

Thanks for the help!

CodePudding user response:

You can do

Yw = df['Year'].astype(str)   df['Week'].astype(str)   '0'
df['Date'] = pd.to_datetime(Yw, format='%Y%U%w')
  • Related