You'll need to convert the datetime.time
objects to timedelta
as well so that the arithmetic works.
Ex:
import datetime
import pandas as pd
# some dummy data:
df = pd.DataFrame({'Time_diff': [pd.Timedelta(hours=1), pd.Timedelta(hours=2), pd.NaT, pd.Timedelta(hours=4)],
'Notif_time': [datetime.time(1,2,3), datetime.time(2,3,4), datetime.time(4,5,6), datetime.time(7,8,9)]})
# Time_diff column and avg_time_diff are of dtype Timedelta...
avg_time_diff = df['Time_diff'].mean()
df['Time_diff'] = df['Time_diff'].fillna(avg_time_diff)
# need to cast Notif_time to Timedelta as well so that the arithmetic works out:
df['Roll_time'] = avg_time_diff + pd.to_timedelta(df['Notif_time'].astype(str))
# df['Roll_time']
# 0 0 days 03:22:03
# 1 0 days 04:23:04
# 2 0 days 06:25:06
# 3 0 days 09:28:09
# Name: Roll_time, dtype: timedelta64[ns]
If you want the output to be of dtype datetime (with all the formatting options etc.), you can get that by adding a date:
# to get from timedelta to datetime, you can add the timedelta column to today's date:
df['roll_datetime'] = pd.Timestamp('now').floor('d') + df['Roll_time']
# df['roll_datetime']
# 0 2021-02-04 03:22:03
# 1 2021-02-04 04:23:04
# 2 2021-02-04 06:25:06
# 3 2021-02-04 09:28:09
# Name: roll_datetime, dtype: datetime64[ns]
Further reading: Format timedelta to string