Вы можете использовать DataFrame.ffill
+ DataFrame.bfill
для завершения с тем же временем входа / выхода:
df[['entry','exit']]=df[['entry','exit']].ffill(axis=1).bfill(axis=1)
print(df)
visitor entry exit
0 A 2016-02-16 08:46:00 2016-02-16 09:01:00
1 A 2016-02-16 09:20:00 2016-02-16 17:24:00
2 A 2016-02-17 09:12:00 2016-02-17 09:42:00
3 A 2016-02-17 09:55:00 2016-02-17 09:55:00
4 A 2016-02-17 12:42:00 2016-02-17 12:56:00
5 A 2016-02-17 13:02:00 2016-02-17 17:32:00
6 A 2016-02-17 17:44:00 2016-02-17 18:24:00
7 A 2016-02-18 07:59:00 2016-02-18 16:40:00
8 A 2016-02-18 16:53:00 2016-02-18 16:53:00
9 A 2016-02-19 09:11:00 2016-02-19 09:11:00
10 A 2016-02-19 09:27:00 2016-02-19 11:26:00
11 A 2016-02-19 12:28:00 2016-02-19 17:12:00
12 A 2016-02-20 08:44:00 2016-02-20 08:58:00
13 A 2016-02-20 09:16:00 2016-02-20 17:21:00
РЕДАКТИРОВАТЬ
DataFrame.notna
+ DataFrame.all
до производительности a boolean indexing
для фильтрации в ro со значениями NaT для расчетасреднее значение разности
#filtering valid data
df_valid=df[df.notna().all(axis=1)]
#Calculating diff
time_dif=df_valid[['entry','exit']].diff(axis=1).exit
print(time_dif)
0 00:15:00
1 08:04:00
2 00:30:00
4 00:14:00
5 04:30:00
6 00:40:00
7 08:41:00
10 01:59:00
11 04:44:00
12 00:14:00
13 08:05:00
Name: exit, dtype: timedelta64[ns]
#Calculatin mean
time_dif_mean=time_dif.mean()
print('This is the mean of time in: ', time_dif_mean)
This is the mean of time in: 0 days 03:26:54.545454
Заполнение значения запотевания средним значением
#roud to seconds( optional)
time_dif_mean_round_second=time_dif_mean.round('s')
df['entry'].fillna(df['exit']-time_dif_mean_round_second,inplace=True)
df['exit'].fillna(df['entry']+time_dif_mean_round_second,inplace=True)
print(df)
Выход:
visitor entry exit
0 A 2016-02-16 08:46:00 2016-02-16 09:01:00
1 A 2016-02-16 09:20:00 2016-02-16 17:24:00
2 A 2016-02-17 09:12:00 2016-02-17 09:42:00
3 A 2016-02-17 09:55:00 2016-02-17 13:21:55
4 A 2016-02-17 12:42:00 2016-02-17 12:56:00
5 A 2016-02-17 13:02:00 2016-02-17 17:32:00
6 A 2016-02-17 17:44:00 2016-02-17 18:24:00
7 A 2016-02-18 07:59:00 2016-02-18 16:40:00
8 A 2016-02-18 16:53:00 2016-02-18 20:19:55
9 A 2016-02-19 05:44:05 2016-02-19 09:11:00
10 A 2016-02-19 09:27:00 2016-02-19 11:26:00
11 A 2016-02-19 12:28:00 2016-02-19 17:12:00
12 A 2016-02-20 08:44:00 2016-02-20 08:58:00
13 A 2016-02-20 09:16:00 2016-02-20 17:21:00