Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate time difference between two dates in the same column in Pandas

Tags:

python

pandas

I have a column (DATE) with multiple data times and I want to find the difference in minutes from date to date and store it into a new column (time_interval).

This is what I have tried:

df['time_interval'] = (df['DATE'],axis=0 - df['DATE'],axis=1) * 24 * 60

These are the data times I am working on

That is the output I am looking for:

like image 658
Ben Avatar asked Oct 16 '25 19:10

Ben


1 Answers

Depending on how you'd care to store the differences, either

df = pd.DataFrame(data=['01-01-2006 00:53:00',
                        '01-01-2006 01:53:00',
                        '01-01-2006 02:53:00',
                        '01-01-2006 03:53:00',
                        '01-01-2006 04:53:00'],
                  columns=['DATE'])
df['DATE'] = pd.to_datetime(df['DATE'])
df['time_interval'] = df['DATE'].diff().fillna(timedelta(0)).apply(lambda x: x.total_seconds() / 60)

to get

                 DATE  time_interval
0 2006-01-01 00:53:00            0.0
1 2006-01-01 01:53:00           60.0
2 2006-01-01 02:53:00           60.0
3 2006-01-01 03:53:00           60.0
4 2006-01-01 04:53:00           60.0

or alternatively

df['time_interval'] = df['DATE'].diff().shift(-1).fillna(timedelta(0)).apply(lambda x: x.total_seconds() / 60)

to get

                 DATE  time_interval
0 2006-01-01 00:53:00           60.0
1 2006-01-01 01:53:00           60.0
2 2006-01-01 02:53:00           60.0
3 2006-01-01 03:53:00           60.0
4 2006-01-01 04:53:00            0.0
like image 141
brentertainer Avatar answered Oct 19 '25 10:10

brentertainer