How to add days and subtract days from dates in Pandas

Learn how to add days and subtract days from dates in Pandas using the Python timedelta function for date addition and date subtraction.

How to add days and subtract days from dates in Pandas
Picture by Karolina Grabowska, Pexels.
4 minutes to read

If you regularly work with time series data, one common thing you’ll need to do is add and subtract days from a date. If you tried doing this by hand, you’d realise that it was actually quite complex, because you have to allow for different month lengths, leap years, and year ends.

Python includes some great features for reformatting dates, so it’s actually quite easy to add and subtract from dates, thanks to the timedelta() function, and it works seamlessly in Pandas. Here’s how to do it.

Load the packages

First, open a Jupyter notebook and import Pandas and the timedelta module from Python’s built-in datetime package. The timedelta function allows you to perform date addition and date subtraction calculations that can add or subtract specified numbers of days from a date and return a new date before or after the original date.

import pandas as pd
from datetime import timedelta

How to subtract days from a date

Since this is the sort of function you may use repeatedly across your projects, I’d recommend creating a couple of helpers function to use within your internal packages or code. First, we’ll create one to subtract days from a date.

The function subtract_days_from_date() will take two arguments, our date string and the number of days to subtract. When the function receives the date string it will first use the Pandas to_datetime() function to convert it to a Python datetime and it will then use the timedelta() function to subtract the number of days defined in the days variable. Finally, it will use strftime() to convert the datetime to the YYYY-MM-DD format I require. You can pass in other arguments to strftime() if you want to return a different date format.

def subtract_days_from_date(date, days):
    """Subtract days from a date and return the date.
    
    Args: 
        date (string): Date string in YYYY-MM-DD format. 
        days (int): Number of days to subtract from date
    
    Returns: 
        date (date): Date in YYYY-MM-DD with X days subtracted. 
    """
    
    subtracted_date = pd.to_datetime(date) - timedelta(days=days)
    subtracted_date = subtracted_date.strftime("%Y-%m-%d")

    return subtracted_date

To run the subtract_days_from_date() function you simply pass in your date string, i.e. 2021-02-01, and the number of days, i.e. 13, and it will return the date 13 days ago, so you can use it elsewhere in your code.

date_subtraction_example = subtract_days_from_date('2021-02-01', 13)
date_subtraction_example
'2021-01-19'

How to add days to a date

The process for adding days to a date in Pandas is pretty much the same. You could handle this in a single function if you wanted to, but the clearer function names of this version improve code readability, I think. The process is exactly the same, but rather than subtracting the timedelta() we add it.

def add_days_to_date(date, days):
    """Add days to a date and return the date.
    
    Args: 
        date (string): Date string in YYYY-MM-DD format. 
        days (int): Number of days to add to date
    
    Returns: 
        date (date): Date in YYYY-MM-DD with X days added. 
    """
    
    added_date = pd.to_datetime(date) + timedelta(days=days)
    added_date = added_date.strftime("%Y-%m-%d")

    return added_date
date_addition_example = add_days_to_date('2021-01-01', 13)
date_addition_example
'2021-01-14'

Matt Clarke, Thursday, August 12, 2021

Matt Clarke Matt is an Ecommerce and Marketing Director who uses data science to help in his work. Matt has a Master's degree in Internet Retailing (plus two other Master's degrees in different fields) and specialises in the technical side of ecommerce and marketing.