'Parse Year Week columns to Date
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!
Solution 1:[1]
You can do
Yw = df['Year'].astype(str) + df['Week'].astype(str) + '0'
df['Date'] = pd.to_datetime(Yw, format='%Y%U%w')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | BENY |