Member-only story

How To Calculate Date Difference Between Rows In Pandas

codeforests
4 min readOct 4, 2020

--

Photo by Aron Visuals on Unsplash

Problem:

You have some data with date (or numeric) data columns, you already knew you can directly use — operator to calculate the difference between columns, but now you would like to calculate the date difference between the two consecutive rows.

For instance, You have some sample data for GPS tracking, and it has the start and end time at each location (latitude and longitude). You would like to calculate the time gap within each location or between two locations.

import pandas as pd 
import numpy as np
df = pd.read_excel("GPS Data.xlsx")
df.head(10)

For a quick view, you can see the sample data output as per below:

Solutions:

Option 1: Using Series or Data Frame diff

Data frame diff function is the most straightforward way to compare the values between the current row and the previous rows. By default, it compare the current and previous row, and you can also specify the period argument in order to compare the current row and current — period row. To calculate the time gap of the start…

--

--

codeforests
codeforests

Written by codeforests

Resources and tutorials for python, data science and automation solutions

No responses yet