Skip to Content

Pandas – Add Days to Date (With Examples)

In this tutorial, we’ll try to solve 2 problems:

  1. Add days to a date in a specific cell of a pandas dataframe.
  2. Add days to all the dates in a pandas datetime type column.

Datatype conversion:

Before going through the below solutions for the problem, we first need to make sure that the datatype of the date_column for which we need to add days, has to be pandas datetime. In case you have strings in the column then try to use pandas.to_datetime method to convert the datatype of the column to pandas datetime.

code:

df['date_column'] = pd.to_datetime(df['date_column'])

The above line of code changes the datatype of the ‘date_column’ column to pandas datetime.

DateOffset function:
Pandas module has a function, DateOffset(days) that adds the specified number of days to the date. This function is used as a solution for the below problem.

Syntax:

pandas.DateOffset(days,...)

Parameters:
days : The number of days to be added to the given date.
Result:
The resulting date after adding the days specified to the given date.

Problem – 1: Add days to date of a specific cell of dataframe

Here, we’re trying to add days to the date in a specific cell of a pandas dataframe object. To achieve this we need to follow the below steps.

  1. Make sure that the datatype of the column is pandas datetime. If not, then use the above step specified in Datatype conversion section
  2. Once you make sure the datatype of the column is pandas datetime, then select the specific cell to which you need to add the days.
  3. Then add the days using the pd.DateOffset(days) function over the selected cell which gives the resultant date after adding the specified days.

Basic Syntax:

df['date_column'] = pd.to_datetime(df['date_column'])
df['date_column'][0]+pd.DateOffset(days=10)

The above code first converts the data type of the column to pandas datetime and then adds 10 days to the date of the specified cell using the pd.DateOffset(days=10) method.

Examples

Now, let us have a look at some of the examples to demonstrate the above solution.

Note:
In case you don’t have any dataframe to try the solution, use the below implementation to create a dataframe with some dates as cells. The below-given dataframe will be used to demonstrate all the below examples.

import pandas as pd
dates = {'date_column':['14 - 05 - 2017', '2017', '07 - 09 - 2019']}
df = pd.DataFrame(dates)
df['date_column'] = pd.to_datetime(df['date_column'], dayfirst = True)
df

Output:

pandas dataframe with a datetime column

Example – 1

code:

df['date_column'][0]+pd.DateOffset(days=10)

Output:

Timestamp('2017-05-24 00:00:00')

Example – 2

code:

df['date_column'][2]+pd.DateOffset(days=50)

Output:

Timestamp('2019-10-27 00:00:00')

Problem – 2: Add days to all the cells in the column of a dataframe

add days to dates in a pandas dataframe column

Here, we’re trying to add days to all the cells of a pandas dataframe column. To achieve this we need to follow the below steps.

  1. Make sure that the datatype of the column is pandas datetime. If not, then use the above step specified in Datatype conversion section.
  2. Once you make sure the datatype of the column is pandas datetime, then select the specific column cell in which you need to add the days.
  3. Then add the days using the pd.DateOffset(days) function over the selected column which gives the resultant dates of the specified column after adding days.

Basic Syntax:

df['date_column'] = pd.to_datetime(df['date_column'])
df['date_column']+pd.DateOffset(days=30)

The above code first converts the data type of the column to pandas datetime and then adds 30 days to all dates of the specified column using the pd.DateOffset(days=30) method.

Examples

Now, let us have a look at some of the examples to demonstrate the above solution.

Note:
In case you don’t have any dataframe to try the solution, use the below implementation to create a dataframe with some dates as cells. The below-given dataframe will be used to demonstrate all the below examples.

import pandas as pd
dates = {'date_column':['14 - 05 - 2017', '2017', '07 - 09 - 2019']}
df = pd.DataFrame(dates)
df['date_column'] = pd.to_datetime(df['date_column'], dayfirst = True)
df

Output:

pandas dataframe with a datetime column

Example – 1

code:

df['date_column']+pd.DateOffset(days=30)

Output:

0   2017-06-13
1   2017-01-31
2   2019-10-07
Name: date_column, dtype: datetime64[ns]

Summary

In this we looked at 2 different problems and tried to solve each one separately

  1. Add days to the date in a specific cell of a pandas dataframe.

    Solution: Using pd.DateOffset(days) method

    Result: Date after specific number of days

  2. Add days to all the cells of a pandas dataframe column.

    Solution: Using pd.DateOffset(days) method

    Result: Series of dates of all cells from the specific column after adding specific number of days

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.