如何获取datetime对象的最大值和最小值

时间:2017-06-19 18:41:32

标签: python-2.7 datetime strptime

我有一个包含多年数据的csv文件,我需要计算两个日期(最大日期和最小日期)之间的差异,所以我认为我应该提取最大日期和最小日期。

这是我的数据:

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

我的代码正如我所说:

 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)

所以,这样做我得到了错误:

  

datetime.datetime'对象不可迭代

我该怎么办?

1 个答案:

答案 0 :(得分:1)

max()min()仅适用于iterables个列表。因此,请将所有日期放入列表中,然后在同一列表中调用maxmin

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

输出:

Start time:  2006-01-01 13:58:43
End time:    2011-12-31 05:03:51
interval:    2189 days, 15:05:08