Resample from monthly to weekly works, but not vice versa - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Resample from monthly to weekly works, but not vice versa (/thread-38957.html) |
Resample from monthly to weekly works, but not vice versa - JaneTan - Dec-14-2022 Hi I have a function that works OK when I tried to re-sample from weekly to monthly data. But it doesn't work when I re-sample from monthly to weekly data. Appreciate if someone could assist Thank you df=pd.read_excel(input_file, sheet_name='weekly', usecols='A:D', na_values='ND', index_col=0, header=0) #df=pd.read_excel(input_file, sheet_name='mthly', usecols='A:D', na_values='ND', index_col=0, header=0) df.index.names = ['Period'] df.index = pd.to_datetime(df.index) def CONVERT(series, freq, obs, technique='CONSTANT', ign='OFF'): if technique=='CONSTANT' and ign=='ON': if obs=='BEGINNING' : q= pd.Series(df[series], index=df.index) groups =q.resample(freq) val = [x[1][0] for x in groups] m=pd.Series(val, groups.indices) return m print(CONVERT('Values','M', 'BEGINNING', 'CONSTANT', 'ON'))WEEKLY INPUT DATA Period Values 20-Jan-89 3 27-Jan-89 4 3-Feb-89 2 10-Feb-89 5 17-Feb-89 3 24-Feb-89 5 3-Mar-89 5 10-Mar-89 6 17-Mar-89 4 24-Mar-89 5 31-Mar-89 7 7-Apr-89 6 14-Apr-89 6 21-Apr-89 4 28-Apr-89 7 5-May-89 5 12-May-89 6 19-May-89 5 26-May-89 7 2-Jun-89 7 Output from code 1989-01-31 3 1989-02-28 2 1989-03-31 5 1989-04-30 6 1989-05-31 5 1989-06-30 7 print(CONVERT('Values','W', 'BEGINNING', 'CONSTANT', 'ON'))MONTHLY INPUT DATA Period Values Jan1989 1 Feb1989 3 Mar1989 7 Apr1989 5 May1989 4 OUTPUT ERROR IndexError: index out of bounds |