How to Count the Number of Days Between a Date and Today in Python Pandas?

Estimated read time 2 min read

To count the number of days between a date and today in Python Pandas, you can use the datetime module and the pd.Timedelta function.

Here’s an example:

import pandas as pd
from datetime import datetime

# create a sample dataframe
df = pd.DataFrame({
    'date': ['2022-04-10', '2022-04-13', '2022-04-16', '2022-04-20']

# convert the 'date' column to datetime format
df['date'] = pd.to_datetime(df['date'])

# calculate the difference between each date and today's date
today =
df['days_since_today'] = (today - df['date']).apply(lambda x: x.days)


In this example, we create a sample Pandas dataframe with a date column containing some dates. We convert the date column to datetime format using the pd.to_datetime() function. We then calculate the difference between each date in the date column and today’s date using the datetime module and the .days attribute of the resulting timedelta object. We apply the .apply() method to convert the timedelta object to an integer representing the number of days.

Finally, we add a new column to the dataframe called days_since_today containing the number of days between each date and today’s date. We print the resulting dataframe to the console. The output will be:

        date  days_since_today
0 2022-04-10                 5
1 2022-04-13                 2
2 2022-04-16                -1
3 2022-04-20                -5

Note that the days_since_today column contains negative values for dates that are in the future compared to today’s date. If you want to count the number of days between today’s date and a specific date regardless of whether it is in the past or future, you can use the abs() function to take the absolute value of the resulting difference.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply