我在Python中有一个名为array_my_date的列表,我需要检测日期的浓度。
标准是:
array_my_date = []
array_my_date.append(pd.to_datetime('2013-06-24 00:00:00'))
array_my_date.append(pd.to_datetime('2013-06-26 00:00:00'))
array_my_date.append(pd.to_datetime('2013-06-27 00:00:00'))
array_my_date.append(pd.to_datetime('2013-06-29 00:00:00'))
array_my_date.append(pd.to_datetime('2013-07-01 00:00:00'))
array_my_date.append(pd.to_datetime('2013-07-03 00:00:00'))
array_my_date.append(pd.to_datetime('2013-07-04 00:00:00'))
array_my_date.append(pd.to_datetime('2013-07-06 00:00:00'))
array_my_date.append(pd.to_datetime('2013-07-07 00:00:00'))
array_my_date.append(pd.to_datetime('2013-07-08 00:00:00'))
array_my_date.append(pd.to_datetime('2015-03-01 00:00:00'))
array_my_date.append(pd.to_datetime('2015-03-04 00:00:00'))
array_my_date.append(pd.to_datetime('2017-09-29 00:00:00'))
array_my_date.append(pd.to_datetime('2017-10-02 00:00:00'))
array_my_date.append(pd.to_datetime('2017-10-06 00:00:00'))
array_my_date.append(pd.to_datetime('2017-10-07 00:00:00'))
array_my_date.append(pd.to_datetime('2017-10-08 00:00:00'))
array_my_date.append(pd.to_datetime('2017-10-09 00:00:00'))
array_my_date.append(pd.to_datetime('2018-12-09 00:00:00'))预期的输出是第一个集中的日期。这就是:
[Timestamp('2013-06-24 00:00:00'), Timestamp('2017-09-29 00:00:00')]发布于 2019-07-15 20:00:40
首先,确保对日期列表进行排序:
dates = sorted(array_my_date)然后,逐步建立一份浓度清单:
concentrations = [[dates[0]]] # initialize our memory with the first date
for date in dates[1:]: # iterate through the rest of the dates
last_date = concentrations[-1][-1] # look at the last date we added
if (date - last_date) <= pd.Timedelta(days=25): # is it close enough to be in the same group?
concentrations[-1].append(date) # if so, then put it in the same group
else: # otherwise,
concentrations.append([date]) # make a new group with it at the head这将产生以下结果:
>>> pprint.pprint(concentrations)
[[Timestamp('2013-06-24 00:00:00'),
Timestamp('2013-06-26 00:00:00'),
Timestamp('2013-06-27 00:00:00'),
Timestamp('2013-06-29 00:00:00'),
Timestamp('2013-07-01 00:00:00'),
Timestamp('2013-07-03 00:00:00'),
Timestamp('2013-07-04 00:00:00'),
Timestamp('2013-07-06 00:00:00'),
Timestamp('2013-07-07 00:00:00'),
Timestamp('2013-07-08 00:00:00')],
[Timestamp('2015-03-01 00:00:00'), Timestamp('2015-03-04 00:00:00')],
[Timestamp('2017-09-29 00:00:00'),
Timestamp('2017-10-02 00:00:00'),
Timestamp('2017-10-06 00:00:00'),
Timestamp('2017-10-07 00:00:00'),
Timestamp('2017-10-08 00:00:00'),
Timestamp('2017-10-09 00:00:00')],
[Timestamp('2018-12-09 00:00:00')]]然后,您可以在每个时间段中选择最早的日期,方法如下:
earliest_of_each = [group[0] for group in concentrations]https://stackoverflow.com/questions/57046352
复制相似问题