converting monthly data to weekly data using multi indexing

It seems like you just need to repeat your data 4 times and adjust the date properly:

df['Date'] = pd.to_datetime(df['Date'])

(df.loc[df.index.repeat(4)]
   .assign(Date=lambda x: x['Date'] + pd.to_timedelta(np.tile(np.arange(4),len(df)), 
                                                      unit='W') )
)

Output:

        Date  value item
0 2020-08-01     10    a
0 2020-08-08     10    a
0 2020-08-15     10    a
0 2020-08-22     10    a
1 2020-08-01     12    d
1 2020-08-08     12    d
1 2020-08-15     12    d
1 2020-08-22     12    d
2 2020-09-01      9    b
2 2020-09-08      9    b
2 2020-09-15      9    b
2 2020-09-22      9    b

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top