遍历日期列表并计算每个间隔之间的天数

时间:2018-10-23 12:29:12

标签: python pandas loops

很抱歉,如果已经问过这个问题,但是我无法解决我的问题。 我想计算日期列表的每个间隔之间的日期数,该日期列表看起来像

dates: 

['06/02/2008', '07/01/2008', '10/12/2007', '05/11/2007', '09/10/2007', '10/09/2007', 
'06/08/2007', '10/07/2007', '04/06/2007', '08/05/2007', '10/04/2007', '12/03/2007',
'05/02/2007', '08/01/2007', '11/12/2006', '06/11/2006', '10/10/2006', '05/09/2006',
 '07/08/2006', '10/07/2006', '05/06/2006', '08/05/2006', '10/04/2006', '13/03/2006',
 '06/02/2006', '09/01/2006', '05/12/2005', '07/11/2005', '11/10/2005', '06/09/2005',
'08/08/2005', '11/07/2005', '06/06/2005', '09/05/2005', '04/04/2005', '07/03/2005',
 '09/02/2005']

我试图做类似delta = dates - dates.shift(-1)的事情 但没有成功。我想得到的结果是一个名为delta的列表,如果我有每个间隔之间的天数的集合。例如,第一个差异'06 / 02/2008'-'07 / 01/2008'= 30天。日期格式为“ dd / mm / yyyy”

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

您可以先转换to_datetime,然后先减去所有值,再减去所有值,最后不减去:

dates = pd.to_datetime(dates, format='%d/%m/%Y')
delta =  dates[:-1] - dates[1:]
print (delta)
TimedeltaIndex(['30 days', '28 days', '35 days', '27 days', '29 days',
                '35 days', '27 days', '36 days', '27 days', '28 days',
                '29 days', '35 days', '28 days', '28 days', '35 days',
                '27 days', '35 days', '29 days', '28 days', '35 days',
                '28 days', '28 days', '28 days', '35 days', '28 days',
                '35 days', '28 days', '27 days', '35 days', '29 days',
                '28 days', '35 days', '28 days', '35 days', '28 days',
                '26 days'],
               dtype='timedelta64[ns]', freq=None)

如果需要整数,请添加TimedeltaIndex.days

delta =  (dates[:-1] - dates[1:]).days
print (delta)
Int64Index([30, 28, 35, 27, 29, 35, 27, 36, 27, 28, 29, 35, 28, 28, 35, 27, 35,
            29, 28, 35, 28, 28, 28, 35, 28, 35, 28, 27, 35, 29, 28, 35, 28, 35,
            28, 26],
           dtype='int64')

delta =  (dates[:-1] - dates[1:]).days.tolist()
print (delta)
[30, 28, 35, 27, 29, 35, 27, 36, 27, 28, 29, 35, 28, 28, 35, 27, 35, 29, 28, 
 35, 28, 28, 28, 35, 28, 35, 28, 27, 35, 29, 28, 35, 28, 35, 28, 26]