Home > database >  How to substract 2 datetime list in python
How to substract 2 datetime list in python

Time:03-20

I have 2 different columns in an excel (which I have imported using pandas in Python) as below

data['issue created'] = ['12/31/2015 11:59:45 PM', '12/31/2015 11:59:44 PM', '12/31/2015 11:59:29 PM', '12/31/2015 11:59:29 PM']

data['issue closed']= ['12/31/2015 11:57:46 PM', '12/31/2015 11:56:58 PM', '12/31/2015 11:56:30 PM', '12/31/2015 11:55:32 PM']

How I can substract these 2 columns to get the new column which gives me the elapsed time?

data['elapsed duration'] = data['issue closed'] - data['issue created']

CodePudding user response:

You need to convert the columns into a datetime type. They are probably an object type right now (essentially a string in pandas).

data['issue created'] = pd.to_datetime(data['issue created'])
data['issue closed'] = pd.to_datetime(data['issue closed'])

This will then let you subtract them into a new column just like you wrote.

CodePudding user response:

You need to convert your date columns to real dates first:

df['issue created'] = pd.to_datetime(df['issue created'])
df['issue closed'] = pd.to_datetime(df['issue closed'])

df['elapsed time'] = df['issue created'] - df['issue closed']

Output:

>>> df
        issue created        issue closed    elapsed time
0 2015-12-31 23:59:45 2015-12-31 23:57:46 0 days 00:01:59
1 2015-12-31 23:59:44 2015-12-31 23:56:58 0 days 00:02:46
2 2015-12-31 23:59:29 2015-12-31 23:56:30 0 days 00:02:59
3 2015-12-31 23:59:29 2015-12-31 23:55:32 0 days 00:03:57
  • Related