Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

     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'])

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading