【发布时间】:2014-06-09 18:46:53
【问题描述】:
我有一个带有日期时间索引的 DataFrame:
tbl.iloc[:,:2].head(5)
date_time var1 var2
2011-01-01 00:05:00 97.97 1009.28
2011-01-01 00:10:00 97.53 1009.53
2011-01-01 00:15:00 97.38 1009.15
2011-01-01 00:20:00 97.23 1009.03
2011-01-01 00:25:00 97.01 1009.03
现在我想选择周一至周五早上 6 点至晚上 7 点、周六早上 6 点至下午 5 点以及周日早上 8 点至下午 5 点。
我可以在一段时间内这样做:
import datetime
selection = tbl.ix[datetime.time(6):datetime.time(19)]
添加工作日条件,即结合时间范围和布尔索引显然不能像我尝试的那样工作:
tbl['weekday'] = tbl.index.weekday
test = tbl[(tbl.ix[datetime.time(6):datetime.time(19)]) & (tbl['weekday'] == 4)]
=> TypeError: 无法将类型“时间戳”与类型“str”进行比较
test = tbl[(tbl.index>datetime.time(6)) (tbl.index>datetime.time(19)) & (tbl['weekday'] == 4)]
=> TypeError: type object 08:00:00
tbl['date'] = tbl.index
test = tbl[(tbl['date']>datetime.time(8)) & (tbl['weekday'] == 4)]
=> ValueError: 无法从参数构造时间戳
我的代码有什么问题?
【问题讨论】:
标签: python datetime pandas indexing