I have a question, How can I extract index property from string variable in pandas? I want to extract particular properties from datetime index in a loop. code attached below:
features_list = ['dayofweek',
'dayofyear',
'day',
'year',
'month',
'daysinmonth',
'is_leap_year',
'is_month_end',
'is_quarter_end',
'is_year_end',
'is_month_start',
'is_quarter_start',
'is_year_start',
'quarter',
'week']
for feat from features_list:
dataset[f'{feat}'] = dataset.index.feat
so if the value in feat variable is day, the string must be 'dataset.index.day'. if it is week, the string must be 'dataset.index.week'?
Use eval to get from string to property.
dataset[f'{feat}'] = eval("dataset.index."+feat )