1

I've got a pandas dataframe 'A' with a column 'date' containing timestamps in the following format:

2020-06-09 13:30:11.359497

I need to calculate the time difference between every line and the previous in minutes. I've tried Timedelta, shift and diff.

Dario Federici
  • 1,228
  • 2
  • 18
  • 40
  • 2
    Did you first convert the dates from type 'string' to type 'datetime'? Then, using timedelta should help. – HarryS Jul 26 '20 at 10:35
  • Please include [`minimal reproducible example`](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – sushanth Jul 26 '20 at 10:51
  • https://stackoverflow.com/questions/16777570/calculate-time-difference-between-pandas-dataframe-indices – Danail Petrov Jul 26 '20 at 10:53

4 Answers4

5

Create a col. for eg. 'next': will store next day's time and 'diff': difference between the timestamps

df['next']=df['A'].shift(periods =1 ,  fill_value=0)
df['diff']=df['A']-df['next']
jtlz2
  • 7,700
  • 9
  • 64
  • 114
Hussain
  • 308
  • 2
  • 7
1

you could also do this in one line (well actually two also counting the copying)

df["diff"] = df.Dates
df["diff"] = df.Dates.shift(periods=-1)- df.Dates
Seitanist
  • 76
  • 3
1
from datetime import datetime

import time

t1 = '09/06/2020 13:30:11.359497'

t2 = '10/06/2020 09:30:12.352452'

# convert t1, t2 to type datetime

date_time_t1 = datetime.strptime(t1, '%d/%m/%Y %H:%M:%S.%f')

date_time_t2 = datetime.strptime(t2, '%d/%m/%Y %H:%M:%S.%f')

# convert date_time_t1, date_time_t2 to Unix timestamp
timestamp_1 = time.mktime(date_time_t1.timetuple())

timestamp_2 = time.mktime(date_time_t2.timetuple())

# the difference in minutes
print(int(timestamp_2 - timestamp_1) / 60)
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
Michael Fu
  • 26
  • 3
1

using pandas functions:

df['next'] = df.Col1.shift(periods=-1, fill_value=0)
df['diff'] = (df.next - df.Col1)/np.timedelta64(1,'m') # converting to minutes, np is numpy module

using user defined function (can have more control and u can make changes as per ur need):

def find_diff(date_list):
    dif_list = []
    for x, y in zip(date_list, date_list[1:]):
        dif_list.append((y-x).total_seconds()/60)
     return pd.Series(dif_list)

df['diff'] = find_diff(list(df.Col1))
AKHacks
  • 207
  • 1
  • 5