I am looking for week start date for entire date frame , with format of dd-mm-yyyy,
Below week number :(src_data[‘WEEK’])
28 29 30 31 32 33 34 35
code :
src_data['firstdayofweek'] = datetime.datetime.strptime(f'{2020}-W{int(src_data['WEEK'] )- 1}-1','%Y-W%W-%w').date()
Output :
Thanks in advance
Advertisement
Answer
You can add a year and weekday as strings and parse to_datetime with the appropriate directives (see also here). If desired, convert to string with strftime:
src_data = pd.DataFrame({'WEEK':[28,29,30,31,32,33,34,35]}) year, weekday = '2020', '1' src_data['DATE'] = pd.to_datetime(year + src_data['WEEK'].astype(str) + weekday, format='%G%V%u').dt.strftime('%d-%m-%Y') # src_data # WEEK DATE # 0 28 06-07-2020 # 1 29 13-07-2020 # 2 30 20-07-2020 # 3 31 27-07-2020 # 4 32 03-08-2020 # 5 33 10-08-2020 # 6 34 17-08-2020 # 7 35 24-08-2020