简体   繁体   中英

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:

在此处输入图片说明

I would like to resample the date level of the multiindex to weekly frequency W-FRI via upsampling, ie, 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.

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.

In current pandas version 0.23.3, your method will lead to a warning:

FutureWarning: fill_method is deprecated to .resample()

the new syntax is .resample(...).last().ffill()

This will not cause warning:

df.unstack(level=1).resample('W-FRI').pad()

It's better to explicitly state the unstack level (in your case level 1 or -1) IMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM