iskandarblue
iskandarblue

Reputation: 7526

Moving to next iteration in nested for loop if condition met

I have two dataframes that I am comparing in a for-loop: df_intervals and df_events. For each event, the loop checks whether the timestamp of the event falls between all of the possible intervals. What I intend for the loop to do is for each event, check each interval, if True, append to the list and move on to the next, if all are False, then append False to the list.

df_events, df_intervals

(  Var2                  ts
 0  bar 2021-02-10 09:04:31
 1  bar 2021-01-29 05:56:17
 2  bar 2021-01-16 15:59:43
 3  bar 2021-01-25 09:40:40
 4  bar 2021-01-27 16:44:57
 5  bar 2021-01-17 13:28:43
 6  bar 2021-02-03 11:46:10
 7  bar 2021-02-02 11:16:49
 8  bar 2021-01-21 17:12:15
 9  bar 2021-01-19 03:44:30,
   Var1            start_ts              end_ts
 0  foo 2021-02-01 20:29:57 2021-02-02 20:29:57
 1  foo 2021-02-03 20:29:57 2021-02-04 20:29:57
 2  foo 2021-02-04 20:29:57 2021-02-05 20:29:57
 3  foo 2021-02-05 20:29:57 2021-02-06 20:29:57
 4  foo 2021-02-06 20:29:57 2021-02-07 20:29:57
 5  foo 2021-02-07 20:29:57 2021-02-08 20:29:57
 6  foo 2021-02-08 20:29:57 2021-02-11 20:29:57
 7  foo 2021-02-08 20:29:57 2021-02-10 20:29:57
 8  foo 2021-02-10 20:29:57 2021-02-11 20:29:57)

I am not sure what I am doing wrong here, as I am setting match=False at the beginning of each event and, if the condition is not met, appending it to the list at the end of the loop.

matches = []
for event in df_events['ts']:
    for idx, a,b,c in df_intervals.itertuples():
        match=False
        if b <= event <= c:
            match=True
            matches.append(match)
            break
        else:
            continue
        matches.append(match)
​

matches
matches
[True, True]

The desired output is:

[True, False, False, False, False, False, False, True, False, False]

Upvotes: 1

Views: 1405

Answers (3)

skrat
skrat

Reputation: 5562

Maybe you should look into list comprehension and any and all functions. Also if you look at the body if your inner loop, you have if/else condition there, and it either breaks or continues. So the last line is unreachable.

def find_matches(events, intervals):
    for event in events['ts']:
        if any(start <= event and event <= end
               for _, _, start, end in intervals.itertuples()):
            yield True
        else:
            yield False

matches = list(find_matches(df_event, df_intervals))

Upvotes: 2

thesturggler
thesturggler

Reputation: 62

Adding on to coreyp_1's answer, your match is set to False at the beginning of each interval, not each event as you want.

Upvotes: 1

coreyp_1
coreyp_1

Reputation: 319

Your last

matches.append(match)

Is probably indented one level too deep. As it is now, the line will never execute because of the break and continue of the preceeding if..else block.

Upvotes: 0

Related Questions