'How to avoid the next days of following year when we use for loop to find consecutive days in python

I improve code to find consecutive days of some event, the data of this event is built by DataFrame. The head of the DataFrame is :

enter image description here

The logic column takes values 0,1 that refer to unhappened and happened.

And then we found consecutive days according to logic column with consecutive 1 value by this code:

data_tuple = []
for v in dataframe[dataframe['logic'] ==1].groupby((dataframe['logic'] != 1).cumsum()):
data_tuple.append(v)

Then we used this loop to find consecutive 3 days and more :

tuple_3Days_And_More = []
for i ,j in data_tuple:
    if len(j['logic'])>2:
        tuple_3Days_And_More.append(j)

The result of these codes as : enter image description here

If you see the tuple two , there is interaction between final consecutive days in 1982 with the starting consecutive days in 1983

How to avoid this problem .



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source