title: How to check if a date is an element of a two- date pair python
body: <p>I have two csv files, file 1 and file 2, which contain different information. The second column of both csv files contains a date. I want to determine if any date in file 2 is contained between a date-time pair from file 1. By this, I mean contained between two consecutive dates from file 1. I also have an additional constraint that I require  the field in column four of file 1  to be non zero.</p>
  
  <p><strong>File 1</strong></p>
  
  <pre><code>42  08/06/2017 00:00    1   15  0
  42  08/06/2017 00:15    5   11  75
  42  08/06/2017 00:30    0   15  0
  42  08/06/2017 00:45    85  475 0
  42  08/06/2017 01:00    125 75  0
  42  08/06/2017 01:15    0   0   0
  42  08/06/2017 01:30    95  475 0
  42  08/06/2017 01:45    0   75  2.625
  42  08/06/2017 02:00    0   15  0
  42  08/06/2017 02:15    0   13.5    1.5
  42  08/06/2017 02:30    0   1.29623 3.15814
  42  08/06/2017 02:45    0   7.5 15
  42  08/06/2017 03:00    0   0   15
  </code></pre>
  
  <p><strong>File 2</strong></p>
  
  <pre><code>42  2017-06-07-232240
  42  2017-06-08-012636
  42  2017-06-08-013811
  42  2017-06-08-014553
  42  2017-06-08-014751
  42  2017-06-08-101332
  42  2017-06-08-101558
  42  2017-06-08-102707
  42  2017-06-08-104039
  42  2017-06-08-105516
  42  2017-06-08-110620
  </code></pre>
  
  <p><strong>Latest Attempt</strong></p>
  
  <pre><code>import numpy as np
  import csv
  from datetime import datetime,date,timedelta
  
  
  def try_parsing_date(text):
  
      for fmt in ('%Y-%m-%d %H:%M','%Y-%m-%d %H:%M:%S', '%d/%m/%Y %H:%M:%s', '%d/%m/%Y %H:%M','%d/%m/%Y','%H:%M:%S','%Y-%m-%d-%H%M%S.%f'):
          try:
              date_strip = datetime.strptime(text, fmt)
              return date_strip 
          except ValueError:
              pass
      raise ValueError('no valid date format found')
  
  def append_dates(a,b):
      date_1_vec = []
      date_2_vec = []
      with open(a) as file1:
          reader1 = csv.reader(file1,delimiter = ',')
          for row in reader:
              date_1_vec.append(datetime.strptime(row[1], "%Y-%m-%d-%H%M%S"))
          with open(b) as file2:
          feed_bin = []
          upd_vec = []
          nothing = [0]
          reader2 = csv.reader(file2,delimiter = ',')              
  
          for row in reader2:
              temp_date = datetime.strptime(row[1], "%Y-%m-%d %H:%M:%S")
              temp_date2 = temp_date + timedelta(minutes=15)
              test_val = float(row[4])
              if any(   (temp_date &lt; dat for dat in date_1_vec) and (temp_date2 &gt; dat for dat in date_1_vec) and (test_val &gt;nothing for nothing in nothing)  ):
                  feed_bin.append(1)
                  val = 1
                  #print("yes")
              else:
                  feed_bin.append(0)
                  val = 0
                  #print("No")
              upd = [row[0],row[1],row[2],val]
              upd_vec.append(upd)
      np.savetxt("outfile.csv",upd_vec, delimiter=",", fmt='%s') 
  
  def main():
      append_dates("file1.csv","file2.csv")
  main()  
  </code></pre>
  
  <p>I have tried a few different ideas but have so far been unsuccessful. The problem with my current approach is that (I think) the condition is always being met as it is searching through all dates in file 1 as opposed to consecutive dates as I require.</p>
  
  <p>Any suggestions on how to modify my code, or a completely new approach would be greatly appreciated!</p>
  
  <p><strong>Update after Jurgy's suggestions - current output:</strong></p>
  
  <pre><code>2017-06-14 13:51:57 is between 2017-06-14 13:45:00 and 2017-06-14 14:00:00
  2017-06-14 13:57:34 is between 2017-06-14 13:45:00 and 2017-06-14 14:00:00
  2017-06-14 13:51:57 is between 2017-06-14 13:45:00 and 2017-06-14 14:00:00
  2017-06-14 13:57:34 is between 2017-06-14 13:45:00 and 2017-06-14 14:00:00
  2017-06-14 13:51:57 is between 2017-06-14 13:45:00 and 2017-06-14 14:00:00
  2017-06-14 13:57:34 is between 2017-06-14 13:45:00 and 2017-06-14 14:00:00
  2017-06-14 13:51:57 is between 2017-06-14 13:45:00 and 2017-06-14 14:00:00
  2017-06-14 13:57:34 is between 2017-06-14 13:45:00 and 2017-06-14 14:00:00
  2017-06-14 16:42:03 is between 2017-06-14 16:30:00 and 2017-06-14 16:45:00
  2017-06-14 16:42:03 is between 2017-06-14 16:30:00 and 2017-06-14 16:45:00
  2017-06-14 16:42:03 is between 2017-06-14 16:30:00 and 2017-06-14 16:45:00
  2017-06-14 16:42:03 is between 2017-06-14 16:30:00 and 2017-06-14 16:45:00
  2017-06-14 16:42:03 is between 2017-06-14 16:30:00 and 2017-06-14 16:45:00
  2017-06-14 16:42:03 is between 2017-06-14 16:30:00 and 2017-06-14 16:45:00
  2017-06-14 16:42:03 is between 2017-06-14 16:30:00 and 2017-06-14 16:45:00
  </code></pre>