user2989314
user2989314

Reputation: 11

Compare a datetime with a datetime range

I am fairly new to coding with Python and have so far managed to google my way out of most issues thanks to the great resources available on this site.

I am writing a program which takes multiple .csv files, strips out the data from each of the initial files into different types of log files and writes these different types of logs into their own .csv.

Now I have stripped the files back I have the need to roll through file A and for each row take the datetime and search file B for the same datetime and copy the relevant data into a new column alongside the initial data. This is nice and easy with a if A == B for loop HOWEVER... each of these logs are written by different computers who's real time clock drifts over time. So what I actually want is to say take the time from file A and search for a corresponding time in file B +/- 30 seconds which is where I am stuck and have been gong around in circles for the last 3/4 hours!

Currently when I run the below code extract I get the following:

---> 35 if (timecode - margin) <= datetime.datetime.date(ssptime) <= (timecode + margin):

TypeError: can't compare datetime.datetime to datetime.date

Thanks in advance!

    import matplotlib.pyplot as plt  # external function need from inside Canopy
import os # external functions
import re # external functions
import matplotlib.patches as mpatches
import csv
import pandas as pd
import datetime


addrbase = "23"
csvnum = [1,2,3,4,5] # CSV number
csvnum2 = [1,2,3,4,5]
senstyp = ['BSL'] #Sensor Type
Beacons = 5





outfile = open('C:\Users\xxx\Canopy\2303_AVG2.csv', 'w') #File to write to
outcsv = csv.writer(outfile, lineterminator='\n')

with open('C:\Users\xxx\Canopy\2303_AVG.csv', 'r') as f: #File read vairable f
    csvread = csv.reader(f, delimiter=',') #Stores the data from file location f in csvread using the delimiter of','
    for row in csvread: #sets up a for loop using the data in csvread
        timecode = datetime.datetime.strptime(row[1],'%Y/%m/%d  %H:%M:%S')#BSL time to datetime.datetime
        margin = datetime.timedelta(seconds = 30)

        with open('C:\Users\xxx\Canopy\2301_SSP_AVG.csv', 'r') as f: #File read vairable f
            csvreadssp = csv.reader(f, delimiter=',')
            for line in csvreadssp:
                ssptime = datetime.datetime.strptime(row[2],'%Y/%m/%d  %H:%M:%S')#
                print ssptime
                if (timecode - margin) <= datetime.datetime.date(ssptime) <= (timecode + margin):  
                    relssp = line[6]
                    print "Time: " + str(timecode) + " SSP: " + str(relssp)
        #try:
                    row.append(relssp) #Calculates the one way travel time of the range and adds a new column with the data
                    outcsv.writerow(row) # Writes file
        #except ValueError: #handles errors from header files
        #    row.append(0) #handles errors from header files
outfile.flush()        
outfile.close()    
print "done"  

Upvotes: 1

Views: 855

Answers (1)

babbageclunk
babbageclunk

Reputation: 8751

You can't compare a datetime representing a specific point in time to a date, which represents a whole day. What time of day should the date represent?

ssptime is already a datetime (because that's what strptime returns) - why are you calling date on it? This should work:

if (timecode - margin) <= ssptime <= (timecode + margin):  

Since your times are all down to second precision, you could also do this:

if abs((ssptime - timecode).total_seconds()) < margin:

I'm not sure which is clearer - I'd probably lean towards the second.

Upvotes: 2

Related Questions