Search code examples
pythonpandasdataframemulti-indexresampling

Resampling (Upsample) Pandas multiindex dataframe


Here is a sample dataframe for reference:

import pandas as pd
import datetime
import numpy as np
np.random.seed(1234)

arrays = [np.sort([datetime.date(2016, 8, 31), datetime.date(2016, 7, 31), datetime.date(2016, 6, 30)]*3),
         ['A', 'B', 'C', 'D', 'E']*5]
df = pd.DataFrame(np.random.randn(15, 4), index=arrays)
df.index.rename(['date', 'id'], inplace=True)

What it looks like:

enter image description here

I would like to resample the date level of the multiindex to weekly frequency W-FRI via upsampling, i.e., copying from the most recent values how='last'. The examples I've seen usually end up aggregating the data (which I want to avoid) after using the pd.Grouper function.

Edit: I have found a solution below, but I wonder if there is a more efficient method.


Solution

  • Edit: I have found a solution:

    df.unstack().resample('W-FRI', how='last', fill_method='ffill')
    

    but I wonder if there's a more efficient way to do this.