import pandas as pd
df = pd.read_csv("network_auth_logs.csv", parse_dates=["timestamp"])
f = df[df.status == "failed"].sort_values("timestamp")
W = pd.Timedelta(minutes=5)
for user, g in f.groupby("user"):
t = g.timestamp.tolist()
start = None
k = 0
for j in range(len(t)):
while t[j] - t[k] >= W:
k += 1
if j - k + 1 >= 5:
if start is not None and t[k] <= end: # окно пересекается — продолжаем инцидент
end = t[j]
else:
if start is not None:
print(user, start, end)
start, end = t[k], t[j]
if start is not None:
print(user, start, end)