我正试图用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数组不支持缓冲,以及一些关于如何解决的技巧的提示。我做错什么了吗?或者这是一个bug?建议的解决方法是什么?
我相信您需要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