Python Forum
Resample from monthly to weekly works, but not vice versa
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Resample from monthly to weekly works, but not vice versa
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python resample by day and get weekstart data JaneTan 0 728 Dec-15-2022, 03:38 AM
Last Post: JaneTan
  Python Resample: How do I keep NaN as NaN? JaneTan 3 2,243 Dec-08-2022, 10:18 PM
Last Post: deanhystad
  Monthly sales, standard deviation Lyle 3 2,488 Aug-07-2020, 10:33 PM
Last Post: Lyle
  Convert weekly sequences to date and time. SinPy 0 1,451 Nov-23-2019, 05:20 PM
Last Post: SinPy
  resample grouping pr0blem olufemig 1 1,969 Nov-06-2019, 10:45 PM
Last Post: Larz60+
  Object exchange from django app to client app and vide versa Man_from_India 1 1,984 Aug-15-2019, 06:03 AM
Last Post: fishhook
  Create a monthly mean column in multiple years fyec 1 4,031 Jun-21-2018, 03:57 AM
Last Post: scidam
  How to copy folder from server to local and vice versa Sachin_d 3 3,396 Nov-04-2017, 12:53 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020