简体   繁体   中英

How to resample DataFrame with MultiIndex

I'm trying to resample a dataframe with a MultiIndex and none of the questions on here seem to answer this question. I have a dataframe with a DateTimeIndex and another column as part of a MultiIndex. I am looking to resample this dataframe to a finer scale and fill the NaN values with forward fill. Here is what I thought would work:

arrays = [[dt.datetime(2020,10,2,1,0),dt.datetime(2020,10,2,1,0), dt.datetime(2020,10,2,2,0),dt.datetime(2020,10,2,2,0)] ,[1 ,2 ,3 ,4 ] ]

values = [i*i for i in range(0,4)]

df = pd.DataFrame(index = arrays ,data = values)

However, I get this error:

Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'MultiIndex'

Any help or pointers in the right direction would be much appreciated

Keep your index as a single DatetimeIndex, then you can resample and recreate the index all you want:

dates = [dt.datetime(2020,10,2,i,0) for i in range(0,5)]
categories = [ i for i in range(0,5)]
values = [i*i for i in range(0,5)]

df = pd.DataFrame({
    'cat': categories,
    'value': values
}, index=dates)

df = df.resample('5T').ffill().set_index('cat', append=True)

If your dataframe is the result of previous operations, remove all but the datetime from the index:

df = (
    df.reset_index(level=1)
      .resample('5T')
      .ffill()
      .set_index('cat', append=True)
)

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