I want to count the length of time a server is stopped from a dataset. I know the downtime but not the duration.

I have this df:

index a b c reboot 2018-06-25 12:51:00 NaN NaN NaN 1 2018-06-25 12:52:00 NaN NaN NaN 0 2018-06-25 12:53:00 NaN NaN NaN 0 2018-06-25 12:54:00 NaN NaN NaN 0 2018-06-25 12:55:00 NaN NaN NaN 0 2018-06-25 12:56:00 NaN NaN NaN 0 2018-06-25 12:57:00 NaN NaN NaN 0 2018-06-25 12:58:00 NaN 0.6 0.6 0 2018-06-25 12:59:00 NaN NaN 0.5 0 2018-06-25 13:00:00 NaN NaN 0.3 0 2018-06-25 13:01:00 2.55 94.879997 0.23 0 2018-06-25 13:02:00 1.17 Nan 0.13 0 2018-06-25 13:03:00 1.08 98.199997 0.10 0 2018-06-25 13:28:00 NaN NaN NaN 1 2018-06-25 13:29:00 NaN NaN NaN 0 2018-06-25 13:30:00 NaN NaN NaN 0 2018-06-25 13:31:00 NaN NaN NaN 0 2018-06-25 13:31:00 0.5 0.2 0.1 0 2018-06-25 13:32:00 NaN NaN NaN 0 2018-06-25 13:33:00 NaN NaN NaN 0 2018-06-25 13:34:00 3 0.6 0.5 0

I want to count the rows where a , b and c are all NaN and reboot == 1 , with the result in this form:

index period reboot 2018-06-25 12:51:00 7 1 2018-06-25 13:28:00 4 1

I have already tried doing it column by column without the reboot condition.

Input:

index a b c reboot 2018-06-25 12:51:00 NaN NaN NaN 1 2018-06-25 12:52:00 NaN NaN NaN 0 2018-06-25 12:53:00 NaN NaN NaN 0 2018-06-25 12:54:00 NaN NaN NaN 0 2018-06-25 12:55:00 NaN NaN NaN 0 2018-06-25 12:56:00 NaN NaN NaN 0 2018-06-25 12:57:00 NaN NaN NaN 0 2018-06-25 12:58:00 NaN NaN NaN 0 2018-06-25 12:59:00 NaN NaN NaN 0 2018-06-25 13:00:00 NaN NaN NaN 0 2018-06-25 13:01:00 2.55 94.879997 0.23 0 2018-06-25 13:02:00 1.17 Nan 0.13 0 2018-06-25 13:03:00 1.08 98.199997 0.10 0 2018-06-25 13:28:00 NaN NaN NaN 1 2018-06-25 13:29:00 NaN NaN NaN 0 2018-06-25 13:30:00 NaN NaN NaN 0

a=df.index b=df.b.values idx0 = np.flatnonzero(np.r_[True, np.diff(np.isnan(b))!=0,True]) count = np.diff(idx0) idx = idx0[:-1] valid_mask = (count>=step) & np.isnan(b[idx]) out_idx = idx[valid_mask] out_num = a[out_idx] out_count = count[valid_mask] outb = zip(out_num, out_count) periodb=list(outb)

Result :