Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

convert yearweek format to date python

i have a dataframe like this (this data in bigquery. And yearweek format is %Y%U (weeks starts sunday):

data = {'yearweek': ['202140', '202139', '202138', '202137'], 'value': [452, 741, 475, 1000]}
df=pd.DataFrame(data)

yearweek   value
202140     452
202139     741
202138     475
202137     1000

expected output:

yearweek   value  date
202140     452    04-10-2021
202139     741    27-09-2021
202138     475    20-09-2021
202137     1000   13-09-2021

i tried this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df['date'] = pd.to_datetime(df.yearweek + '0', format='%Y-%W%w')

but getting this error:

ValueError: time data '2021420' does not match format '%Y-%W%w' (match)

tried too:

df['datex'] = pd.to_datetime(df.yearweek , format='%Y-%W%w')

but error again

ValueError: time data '202142' does not match format '%Y-%W%w' (match)

>Solution :

You can use format as format='%Y%U%w'

>>> import pandas as pd
>>> df = pd.DataFrame({'yearweek': ['202140', '202139', '202138', '202137'], 'value': [452, 741, 475, 1000]})
>>> df
  yearweek  value
0   202140    452
1   202139    741
2   202138    475
3   202137   1000
>>> df['date'] = pd.to_datetime(df.yearweek + '0', format='%Y%U%w')
>>> df
  yearweek  value       date
0   202140    452 2021-10-03
1   202139    741 2021-09-26
2   202138    475 2021-09-19
3   202137   1000 2021-09-12
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading