首页 > 解决方案 > 填充日期时间索引时的值错误

问题描述

我正在尝试用 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 数组不支持缓冲,以及一些关于黑客可以解决的提示。难道我做错了什么?或者这是一个错误?推荐的解决方法是什么?

标签: pandasnumpydatetime

解决方案


我相信您需要reset_indexDatetimeIndex 二级,然后使用您的解决方案:

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

推荐阅读