Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the max and min value of a datetime object

I have a csv file containing years of data, and I need to calculate the difference between two dates (the max date and the min date), so I believe that I should extract the max date and the min date.

Here's my data :

timestamp,heure,lat,lon,impact,type
2006-01-01 00:00:00,13:58:43,33.837,-9.205,10.3,1
2006-01-02 00:00:00,00:07:28,34.5293,-10.2384,17.7,1
2007-02-01 00:00:00,23:01:03,35.0617,-1.435,-17.1,2
2007-02-02 00:00:00,01:14:29,36.5685,0.9043,36.8,1
....
2011-12-31 00:00:00,05:03:51,34.1919,-12.5061,-48.9,1

I am proceeding as bellow in my code :

 W=np.loadtxt(dataFile,delimiter=',',dtype={'names': ('datum','timestamp','lat','lon','amp','ty'),
                            'formats':('S10',     'S8'   ,'f4' ,'f4' ,'f4','S3' )})

 day = datetime.strptime(W['datum'][0],'%Y-%m-%d')
 time=[]
 for i in range(W.size):
 timestamp = datetime.strptime(W['datum'][i]+' '+W['timestamp'][i],'%Y-%m-%d %H:%M:%S')
 Tempsfinal = max(timestamp)
 Tempsinitial = min(timestamp)
 interval=int((Tempsfinal- Tempsinitial)/6)

So, doing this I got the error:

datetime.datetime' object is not iterable

How can I proceed?

like image 981
Mar Avatar asked Jun 19 '17 18:06

Mar


1 Answers

max() and min() only work with iterables like lists. So put all the dates into a list, then call max and min on that same list:

all_timestamps = []

for i in range(W.size):
    try:
        timestamp = datetime.strptime(W['datum'][i]+' '+W['timestamp'][i],'%Y-%m-%d %H:%M:%S')
        all_timestamps.append(timestamp)  # make a list of dates
        # Tempsfinal = max(timestamp)                 # ###
        # Tempsinitial = min(timestamp)               # move this out of loop
        # interval=int((Tempsfinal- Tempsinitial)/6)  # ### 

    except ValueError,e:
        print "error",e,"on line",i

# get min/max
Tempsfinal = max(all_timestamps)   
Tempsinitial = min(all_timestamps)

interval = Tempsfinal-Tempsinitial

print 'Start time: ', Tempsinitial
print 'End time:   ', Tempsfinal
print 'interval:   ', interval

output:

Start time:  2006-01-01 13:58:43
End time:    2011-12-31 05:03:51
interval:    2189 days, 15:05:08
like image 197
chickity china chinese chicken Avatar answered Sep 26 '22 01:09

chickity china chinese chicken