2

I have dataframe that look like this.

| Date_Time           | Execution_Time |  
|---------------------|----------------|  
| 2019-10-10 09:07:29 | 14.0           |
| 2019-09-21 19:47:01 | 14.3           |
| 2019-09-19 02:49:49 | 14.1           |
| 2019-09-27 23:19:16 | 21.9           |
| 2019-09-05 18:46:00 | 14.2           |

The execution is in seconds. How can I add Date_Time and Execution_Time ?

datatype for Date_Time: object, Execution_Time: float64

I have tried df['diff'] = df['Date_Time'] + df['Execution_Time'] and it returns following error:

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')

1 Answer 1

4

First column convert to datetimes by to_datetime and second to timedeltas by to_timedelta:

df['diff'] = (pd.to_datetime(df['Date_Time']) + 
              pd.to_timedelta(df['Execution_Time'], unit='s'))
print (df)
             Date_Time  Execution_Time                    diff
0  2019-10-10 09:07:29            14.0 2019-10-10 09:07:43.000
1  2019-09-21 19:47:01            14.3 2019-09-21 19:47:15.300
2  2019-09-19 02:49:49            14.1 2019-09-19 02:50:03.100
3  2019-09-27 23:19:16            21.9 2019-09-27 23:19:37.900
4  2019-09-05 18:46:00            14.2 2019-09-05 18:46:14.200
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for helping me. previously i tried newdf[["Date_Time","Execution_Time"]] = newdf[["Date_Time","Execution_Time"]].apply(pd.to_datetime) to convert both to date time.. but the 'Execution_time' Column value got messed up.. Thank you again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.