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:
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)
CodePudding user response:
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