Error when joining two time columns with the + operator

Be the following python pandas DataFrame. I want to merge the two columns into one to create the full datetime format.

     num_plate_ID     cam  entry_date entry_time other_columns
0             XYA       2  2022-02-14   23:20:21     ...
1             JDS       2  2022-02-12   23:20:21     ...
2             OAP       0  2022-02-05   14:30:21     ...
3             ASI       1  2022-04-07   15:30:21     ...

However, I get this error.

df['entry'] = df['entry_date'] + " " +  df['entry_time']
df['entry'] = pd.to_datetime(df['entry'])
# TypeError: unsupported operand type(s) for +: 'datetime.date' and 'str'

I want to get this result.

     num_plate_ID     cam  entry_date entry_time                   entry   other_columns
0             XYA       2  2022-02-14   23:20:21     2022-02-14 23:20:21
1             JDS       2  2022-02-12   23:20:21     2022-02-12 23:20:21  
2             OAP       0  2022-02-05   14:30:21     2022-02-05 14:30:21  
3             ASI       1  2022-04-07   15:30:21     2022-04-07 15:30:21  

>Solution :

you can use:

df['entry'] = pd.to_datetime(df['entry_date'].astype(str) + " " +  df['entry_time'])

Leave a Reply