[英]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.我正在尝试使用 MultiIndex 重新采样数据帧,这里的问题似乎都没有回答这个问题。 I have a dataframe with a DateTimeIndex and another column as part of a MultiIndex.我有一个带有 DateTimeIndex 和另一列作为 MultiIndex 一部分的数据框。 I am looking to resample this dataframe to a finer scale and fill the NaN values with forward fill.我希望将此数据帧重新采样到更精细的比例,并使用前向填充填充 NaN 值。 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:将您的索引保留为单个 DatetimeIndex,然后您可以根据需要重新采样和重新创建索引:
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)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.