查找两个日期时间点之间的所有数据

时间:2014-07-07 22:19:06

标签: python arrays datetime matplotlib

我正在尝试制作一个iPython笔记本,用于绘制两个日期时间点之间的所有数据。我已经完成了大部分工作,我只需要这样做,这样我就可以选择开始日期和结束日期。该程序的工作是将所有数据存储到变量中。

这是一段数据(请记住,我有240,000个数据点)

[datetime.datetime(2014, 6, 27, 17, 40, 55), datetime.datetime(2014,
6, 27, 17, 40, 56), datetime.datetime(2014, 6, 27, 17, 40, 57),
datetime.datetime(2014, 6, 27, 17, 40, 58), datetime.datetime(2014, 6,
27, 17, 40, 59), datetime.datetime(2014, 6, 27, 17, 41),
datetime.datetime(2014, 6, 27, 17, 41, 1), datetime.datetime(2014, 6,
27, 17, 41, 2), datetime.datetime(2014, 6, 27, 17, 41, 3),
datetime.datetime(2014, 6, 27, 17, 41, 4), datetime.datetime(2014, 6,
27, 17, 41, 5), datetime.datetime(2014, 6, 27, 17, 41, 6),
datetime.datetime(2014, 6, 27, 17, 41, 7), datetime.datetime(2014, 6,
27, 17, 41, 8), datetime.datetime(2014, 6, 27, 17, 41, 9),
datetime.datetime(2014, 6, 27, 17, 41, 10), datetime.datetime(2014, 6,
27, 17, 41, 11), datetime.datetime(2014, 6, 27, 17, 41, 12)]

大部分格式化(年,月,日,24小时,分钟,秒)。

我怎样才能做到:

startYear = 2014 
startMonth = 6 
startDay = 27 
startHour = 17
startMinute = 40 
startSecond = 55

endYear = 2014 
endMonth = 6 
endDay = 28 
endHour = 17 
endMinute = 40
endSecond = 55

我在数据块上使用date2num,我认为在所需日期使用date2num只会交叉搜索相同的数字。我只需要最终结果如下:

BetweenTime = (StartTime):(EndTime)

这样我就可以将BetweenTime插入到我的图表中。

感谢任何帮助,我对Python来说相对较新。

1 个答案:

答案 0 :(得分:1)

日期时间对象具有可比性(请参阅https://docs.python.org/2/library/datetime.html#datetime.datetime.date中的“datetime1< datetime2比较日期时间与日期时间。(4)”),以便您可以这样做:

sDate = datetime(starYear,...,startSecond) #fill this in
eDate = datetime(endYear,...,endSecond) #fill this in
for i in data:
    if i >= sDate and i <= eDate:
         #DO SOMETHING WITH i IT SHOULD BE INCLUDED