-1

I have a list of DateTime objects, and I want to compare if any two of them has the same Hour and Minutes or at most 2 minutes apart. I compare them by using the itertools.combinations( data, 2 ). But I am stuck in the comparison part. I am not sure how to compare 2 minutes or more because if one timestamp is 11:58 two minutes more will be 12:00, which means I can add 2 to 11:58 as it will go to 11:60 instead of 12:00.

I converted the dateTime objects to string with this

dt1 = int((acqList[a].timestamp).time().strftime('%H%M'))
dt2 = int((acqList[b].timestamp).time().strftime('%H%M'))

Then, I did this

if dt1 == dt2 or dt1+1 == dt2 or dt2+1 == dt1 or dt1-1 == dt2 or dt2-1 == dt1:

but this will lead to the concern of 1160 and did not include at most 2 minutes difference

example of the dateTime object List:

2019-10-01 15:37:11+00:00
2019-10-01 15:37:09+00:00
2019-10-01 16:09:06+00:00
2019-10-01 15:24:55+00:00
2019-10-01 16:36:56+00:00
2019-10-01 10:08:39+00:00
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
xd3262nd
  • 7
  • 1
  • 5
  • Does this answer your question? [How to check if two datetimes are within a certain range of each other?](https://stackoverflow.com/questions/42635426/how-to-check-if-two-datetimes-are-within-a-certain-range-of-each-other) – AMC Mar 17 '20 at 17:09

2 Answers2

3

Do not pass to string, numeric values are easier to compare.

From two datetime object you can compare them, in each way using this method

d1 = datetime.now()
d2 = datetime.now() - timedelta(minutes=2)

if fabs((d1 - d2).total_seconds()) <= 120:
    print("diff lower than 2m")

The following would fit but in only one way :

if (d1 - d2) <= timedelta(minutes=2):
    print("diff lower than 2m")

To find if a list of datetime are in a 2-min interval, check that every combination is lower than 2min

values = [datetime.now() - timedelta(minutes=randint(0, 2)) for i in range(20)]
combi = combinations(values, 2)
if all([fabs((d1 - d2).total_seconds()) <= 120 for d1, d2 in combi]):
    print("All values fit in a 2-min interval")
azro
  • 53,056
  • 7
  • 34
  • 70
  • Gotcha. So do I still need to convert the datetime object into string or int? – xd3262nd Mar 17 '20 at 15:54
  • @xd3262nd into none of them, You want every value to be in a 2-min interval ? – azro Mar 17 '20 at 15:55
  • yea. I want to check if they are in 2-min interval. I think I get it now. Thanks so much for your help! Learning new things now – xd3262nd Mar 17 '20 at 16:00
  • Do you mind to explain this code to me? ```if all([fabs((d1 - d2).total_seconds()) <= 120 for d1, d2 in combi]): print("All values fit in a 2-min interval")``` I ran it but it does not show any print statement at all – xd3262nd Mar 17 '20 at 16:12
  • @xd3262nd If for all combination of values, the difference between the 2 is lower than 2min, you'll the print. With the previous line I build a list of datetime that between now, and 2min before now, so ALL are in the same 2-3min intervall and the condition will be true – azro Mar 17 '20 at 16:14
  • right but it does not show any print statement on my end when I'm trying to run your example code above – xd3262nd Mar 17 '20 at 16:17
  • @xd3262nd Did you increase the size of the list ? use randint(0, 1), maybe your list tak to much time to build, and one value is out of the interval – azro Mar 17 '20 at 16:20
  • Yea, it works now I changed that and the range to (10). Thanks again. – xd3262nd Mar 17 '20 at 16:22
  • Hi, sorry so I am facing some issues and thinking I might change my approach to get the least difference of ```fabs(d2-d1)``` instead of ```if fabs((d1 - d2).total_seconds()) <= 120:``` but I am not sure how I can approach that – xd3262nd Mar 17 '20 at 19:08
  • @xd3262nd you need to keep total_seconds(), you can't do absolute of timedelta – azro Mar 17 '20 at 19:25
0

Using datetime objects, you can calculate the difference directly substracting the value from the shifted column:

>>>df = pd.DataFrame(index = range(7), columns = ['date'], 
>>>data=['2012-03-16 23:50:00',
'2012-03-16 23:56:00',
'2012-03-17 00:08:00',
'2012-03-17 00:10:00',
'2012-03-17 00:12:00',
'2012-03-17 00:20:00',
'2012-03-20 00:43:00'])
>>>df.date = pd.to_datetime(df['date'])


>>>df['delta'] = (df['date']-df['date'].shift()).fillna(pd.Timedelta(seconds=0))

    date    delta
0   2012-03-16 23:50:00 0 days 00:00:00
1   2012-03-16 23:56:00 0 days 00:06:00
2   2012-03-17 00:08:00 0 days 00:12:00
3   2012-03-17 00:10:00 0 days 00:02:00
4   2012-03-17 00:12:00 0 days 00:02:00
5   2012-03-17 00:20:00 0 days 00:08:00
6   2012-03-20 00:43:00 3 days 00:23:00
TitoOrt
  • 1,265
  • 1
  • 11
  • 13