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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With