我试图用DateTimeIndex填充数据框上的日期。这是获取初始数据帧的设置:
days = (date(2018,8,5),date(2018,8,6),date(2018,8,9))
colors = ('red','red','blue')
tuples = list(zip(days,colors))
index = pd.MultiIndex.from_tuples(tuples,names=['day','color'])
df = pd.DataFrame(np.random.randn(3,2) \
,index=index,columns=['first','second'])
产生此数据帧:
first second
day color
2018-08-05 red 0.044029 1.135556
2018-08-06 red 0.212579 -0.157853
2018-08-09 blue -0.502317 -0.019823
现在要重新编制索引以填写缺失的日期:
start = df.index.get_level_values('day').min()
end = df.index.get_level_values('day').max()
reindexer = pd.date_range(start,end)
df2 = df.groupby('color').apply(lambda x: x.reindex(reindexer))
会产生此错误:
ValueError: cannot include dtype 'M' in a buffer
在线上有几篇文章描述了此消息,这是由于datetime64数组不支持缓冲,以及一些有关变通办法的提示。难道我做错了什么?还是这是一个错误?建议的解决方法是什么?
答案 0 :(得分:0)
我相信您需要reset_index
到第二级的DatetimeIndex,然后才能解决您的问题:
df2 = (df.reset_index(level=1)
.groupby('color')['first','second']
.apply(lambda x: x.reindex(reindexer)))
print (df2)
first second
color
blue 2018-08-05 NaN NaN
2018-08-06 NaN NaN
2018-08-07 NaN NaN
2018-08-08 NaN NaN
2018-08-09 -0.917287 -1.115499
red 2018-08-05 0.182462 0.205502
2018-08-06 0.541304 -1.525548
2018-08-07 NaN NaN
2018-08-08 NaN NaN
2018-08-09 NaN NaN
或者:
start = df.index.get_level_values('day').min()
end = df.index.get_level_values('day').max()
colors = df.index.get_level_values('color').unique()
dates = pd.date_range(start,end)
mux = pd.MultiIndex.from_product([dates, colors], names=['day','color'])
df = df.reindex(mux)
print (df)
first second
day color
2018-08-05 red -0.181284 0.162945
blue NaN NaN
2018-08-06 red 0.920916 0.691335
blue NaN NaN
2018-08-07 red NaN NaN
blue NaN NaN
2018-08-08 red NaN NaN
blue NaN NaN
2018-08-09 red NaN NaN
blue 1.286144 -2.356252