Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between two dates in Pandas DataFrame

I have many columns in a data frame and I have to find the difference of time in two column named as in_time and out_time and put it in the new column in the same data frame.

The format of time is like this 2015-09-25T01:45:34.372Z.

I am using Pandas DataFrame.

I want to do like this:

df.days = df.out_time - df.in_time


I have many columns and I have to increase 1 more column in it named days and put the differences there.

like image 832
Abhishek Shankhadhar Avatar asked Mar 20 '26 16:03

Abhishek Shankhadhar


1 Answers

You need to convert the strings to datetime dtype, you can then subtract whatever arbitrary date you want and on the resulting series call dt.days:

In [15]:
df = pd.DataFrame({'date':['2015-09-25T01:45:34.372Z']})
df

Out[15]:
                       date
0  2015-09-25T01:45:34.372Z

In [19]:
df['date'] = pd.to_datetime(df['date'])
df['day'] = (df['date'] - dt.datetime.now()).dt.days
df

Out[19]:
                     date  day
0 2015-09-25 01:45:34.372 -252
like image 76
EdChum Avatar answered Mar 22 '26 08:03

EdChum