Member-only story
How To Calculate Date Difference Between Rows In Pandas
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…