Error when joining two time columns with the + operator

Question:

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  
Asked By: Carola

||

Answers:

you can use:

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

Answered By: Clegane
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.