Your logic is over-complicated. Row-wise loops, which is what pd.DataFrame.apply represents, should be actively avoided with Pandas. Here, you can convert a timedelta series to seconds, then take the absolute value:
df = pd.DataFrame({'a_time': pd.to_datetime(['2018-01-01 05:32:00', '2018-05-10 20:13:41']),
'b_time': pd.to_datetime(['2018-01-01 15:10:05', '2018-05-10 16:09:16'])})
df['C'] = (df['b_time'] - df['a_time']).dt.total_seconds().abs() / 60
print(df)
a_time b_time C
0 2018-01-01 05:32:00 2018-01-01 15:10:05 578.083333
1 2018-05-10 20:13:41 2018-05-10 16:09:16 244.416667
For academic purposes, this is how you would use apply:
def time_delta(row):
if row['a_time'] > row['b_time']:
return (row['a_time'] - row['b_time']) / np.timedelta64(1, 'm')
else:
return (row['b_time'] - row['a_time']) / np.timedelta64(1, 'm')
df['C'] = df.apply(time_delta, axis=1)
Notice, in both versions, we assume you are starting with datetime series. If this isn't case, make sure you convert to datetime as an initial step:
time_cols = ['a_time', 'b_time']
df[time_cols] = df[time_cols].apply(pd.to_datetime)