A simple way to finding the difference between two dates in Pandas

Bram Tunggala
2 min readAug 29, 2019

--

I recently completed a data science bootcamp. I can manipulate columns, rows, create machine learning models, and and explain to you what maximum likelihood estimation means… but if you ask me how to take the difference between two dates, I would not know how to do this efficiently.

This led me to write about… timedelta or the difference between two dates. I find using Python’s datetime along with Pandas, very convenient any easy to use.

We’re going to start with generating some dummy time data and importing our necessary libraries. Then finding the difference between two dates in:

  • days
  • weeks
  • months
  • years

Finding the difference between two dates in days

Finding the difference between two dates in weeks

Finding the difference between two dates in months

Finding the difference between two dates in years

As you can see, the pattern goes like this:

  • first, calculate the difference between the two dates
  • second, convert the difference in the metric you want to use… ‘D’ for day, ‘W’ for weeks, ‘M’ for month, ‘Y’ for year

I hope this was easy to follow. Shout out to DataScience Made Simple!

--

--

Responses (1)