I am posting code for 5 mins so that output matches your desired output. Change 300 to 900 for 15 mins.
Steps:
Convert the df['Date'] to datetime so that we can subtract two dates
to know the time difference between them.
Group the df by date and Call f for each group object.
In f: max-continuous_range gives the
length of longest segment where value is 50. f return True if length is 5 mins or more. Append date in list if f returns True.
Use:
def f(g):
mask = (g['Value'] == 50)
max_continuous_range = (np.max(np.cumsum(g['Date'].where(mask).diff()))
+ timedelta(minutes = 1))
return max_continuous_range.seconds >= 300
df['Date'] = pd.to_datetime(df['Date'])
groups = df.groupby(df['Date'].dt.date, as_index = False)
final_list = [str(idx) for idx, g in groups if f(g)]
Input:
Date Value
0 2020-01-01 01:35 40
1 2020-01-01 01:36 50
2 2020-01-01 01:37 50
3 2020-01-01 01:38 50
4 2020-01-01 01:39 50
5 2020-01-01 01:40 50
6 2020-01-01 01:41 40
7 2020-01-01 01:42 40
Output:
>>> final_list
['2020-01-01']
Inside f(g):
mask: True where value is 50.
0 False
1 True
2 True
3 True
4 True
5 True
6 False
7 False
df['Date'].where(mask) Puts NaT where mask is not True.
0 NaT
1 2020-01-01 01:36:00
2 2020-01-01 01:37:00
3 2020-01-01 01:38:00
4 2020-01-01 01:39:00
5 2020-01-01 01:40:00
6 NaT
7 NaT
.diff gives difference between two consecuting elements. It will give NaT if any value is NaT. Result after df['Date'].where(mask).diff():
0 NaT
1 NaT
2 0 days 00:01:00
3 0 days 00:01:00
4 0 days 00:01:00
5 0 days 00:01:00
6 NaT
7 NaT
Now cumulative sum of difference between consecutive times will give us the total time elapsed. After np.cumsum(...):
0 NaT
1 NaT
2 0 days 00:01:00
3 0 days 00:02:00
4 0 days 00:03:00
5 0 days 00:04:00
6 NaT
7 NaT
np.max gives us the longest length. 1 minute is added to take care of boundary condition