Difference between two Timestamps in Seconds, Minutes, hours in Pandas python

Difference between Timestamps in pandas can be achieved using timedelta function in pandas. In this section we will be covering difference between two dates / Timestamps in Seconds, Minutes, hours  and nano seconds in pandas python with example for each. We will be explaining how to get

  • Difference between two Timestamps in Seconds – calculate seconds between two timestamp in pandas dataframe python
  • Difference between two Timestamps in Minutes – calculate minutes between two timestamp in pandas dataframe python
  • Difference between two Timestamps in hours – calculate hours between two timestamp in pandas  dataframe python
  • Difference between two Timestamps in Nano Seconds – calculate nano seconds between two timestamp in pandas  dataframe python

First let’s create a dataframe with two dates.

import pandas as pd
import numpy as np
import datetime
from dateutil.relativedelta import relativedelta
from datetime import date


date1 = pd.Series(pd.date_range('2012-1-1 12:00:00', periods=7, freq='M'))
date2 = pd.Series(pd.date_range('2013-3-11 21:45:00', periods=7, freq='W'))

df = pd.DataFrame(dict(Start_date = date1, End_date = date2))
print(df)

so the resultant dataframe will be

Difference between two dates in days , weeks, Months and years in Pandas python 1

 

Difference between two timestamps in seconds – pandas dataframe python

  • First line calculates the difference between two timestamps
  • Second line converts the difference in terms of seconds (timedelta64(1,’s’)- small s indicates seconds)
df['diff_seconds'] = df['End_date'] - df['Start_date']
df['diff_seconds']=df['diff_seconds']/np.timedelta64(1,'s')

print(df)

so the resultant dataframe will be

 

Difference between two timestamps in Minutes – pandas dataframe python

  • First line calculates the difference between two timestamps
  • Second line converts the difference in terms of minutes (timedelta64(1,’m’)- small m indicates minutes)
df['diff_minutes'] = df['End_date'] - df['Start_date']
df['diff_minutes']=df['diff_minutes']/np.timedelta64(1,'m')

print(df)

so the resultant dataframe will be

 

Difference between two timestamps in Hours – pandas dataframe python

  • First line calculates the difference between two timestamps
  • Second line converts the difference in terms of hours (timedelta64(1,’h’)- small h indicates hours)
df['diff_hours'] = df['End_date'] - df['Start_date']
df['diff_hours']=df['diff_hours']/np.timedelta64(1,'h')

print(df)

so the resultant dataframe will be

 

Difference between two timestamps in Nano seconds – pandas dataframe python

  • First line calculates the difference between two timestamps
  • Second line converts the difference in terms of Nano seconds (timedelta64(1,’ns’)- small ns indicates nano seconds)
df['diff_nano_seconds'] = df['End_date'] - df['Start_date']
df['diff_nano_seconds']=df['diff_nano_seconds']/np.timedelta64(1,'ns')

print(df)

so the resultant dataframe will be

 

Difference between two Timestamps in Seconds, Minutes, hours in Pandas python                                                                                                          Difference between two Timestamps in Seconds, Minutes, hours in Pandas python

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts