pandas - 填充日期时间索引时的值错误
问题描述
我正在尝试用 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 数组不支持缓冲,以及一些关于黑客可以解决的提示。难道我做错了什么?或者这是一个错误?推荐的解决方法是什么?
解决方案
我相信您需要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
推荐阅读
- javascript - 如何在获取服务方法中进行验证,而不是在角度 9 中返回整个 json 数据
- java - 如何在Java中的特定时间后中断方法?
- javascript - 表单响应值总是给出未定义,最终尝试推送到后端,尝试了 2 种方式
- assembly - elf intel 32 输出不打印
- rust - 如何找到 rustc --target=$TARGET 将链接到哪个 libc.so?
- node.js - 无法访问docker容器内的mongodb
- javascript - 如何创建可单击的数据表行以及单击时根据行中的值移动到不同的页面
- java - java.lang.NullPointerException:尝试在空对象reference.parse上调用虚拟方法'java.io.File com.parse.ParsePlugins.getParseDir()'
- excel - 使用 VBA 动态显示选择的服务总量 - 购物车
- sql - MariaDB:将查询结果导出到 csv 文件时缺少一些列