我有一个日期时间对象列表,并希望找到在特定时间范围内的对象:
import datetime
dates = [ datetime.datetime(2007, 1, 2, 0, 1),
datetime.datetime(2007, 1, 3, 0, 2),
datetime.datetime(2007, 1, 4, 0, 3),
datetime.datetime(2007, 1, 5, 0, 4),
datetime.datetime(2007, 1, 6, 0, 5),
datetime.datetime(2007, 1, 7, 0, 6) ]
#in reality this is a list of over 25000 dates
mask = (dates>datetime.datetime(2007,1,3)) & \
(dates<datetime.datetime(2007,1,6))
但是,这会导致以下错误: “TypeError:无法将datetime.datetime与列表进行比较”
如何修复我的代码?
答案 0 :(得分:12)
您可以在描述(但不是列表)的语法中屏蔽numpy.array
:
import numpy as np
date1 = np.array(dates)
mask = (dates1 > datetime.datetime(2007,1,3)) & \
(dates1 < datetime.datetime(2007,1,6))
In [14]: mask
Out[14]: array([False, True, True, True, False, False], dtype=bool)
In [15]: dates1[mask]
Out[15]: array([2007-01-03 00:02:00, 2007-01-04 00:03:00, 2007-01-05 00:04:00], dtype=object)
(因为这个问题被标记为numpy,大概这就是你想要的。)
答案 1 :(得分:10)
如果您的dates
列表按排序顺序排列,则可以使用bisect
module:
>>> import bisect
>>> bisect.bisect_right(dates, datetime.datetime(2007,1,3))
1
>>> bisect.bisect_left(dates, datetime.datetime(2007,1,6))
4
.bisect_*
函数将索引返回到dates
列表:
>>> lower = bisect.bisect_right(dates, datetime.datetime(2007,1,3))
>>> upper = bisect.bisect_left(dates, datetime.datetime(2007,1,6))
>>> mask = dates[lower:upper]
>>> mask
[datetime.datetime(2007, 1, 3, 0, 2), datetime.datetime(2007, 1, 4, 0, 3), datetime.datetime(2007, 1, 5, 0, 4)]
答案 2 :(得分:5)
import datetime
dates = [ datetime.datetime(2007, 1, 2, 0, 1),
datetime.datetime(2007, 1, 3, 0, 2),
datetime.datetime(2007, 1, 4, 0, 3),
datetime.datetime(2007, 1, 5, 0, 4),
datetime.datetime(2007, 1, 6, 0, 5),
datetime.datetime(2007, 1, 7, 0, 6) ]
within = [date for date in dates if datetime.datetime(2007,1,3) < date < datetime.datetime(2007,1,6)]
的产率:
[datetime.datetime(2007, 1, 3, 0, 2),
datetime.datetime(2007, 1, 4, 0, 3),
datetime.datetime(2007, 1, 5, 0, 4)]