Search code examples
pythonexcelpandasdatedelta

Read date from Excel file and get a delta with today's date


I have an excel file with some dates in it. They're formatted with dd-mm-yyyy. I would like to take each date in that column and get a delta of it with today's date.

For example, if the Excel file has a date as so: 20-6-2019, it will subtract today's date (16/6/2019) and return 4.

I've been trying to approach this with pandas and with xlrd, but each time I'm getting overwhelmed and just couldn't understand what I'm doing.

This was my approach:

import pandas as pd
import datetime

file_name = pd.read_excel (r'C:\Users\Daniel Beilin\VSCodeProjects\ExcelReminder\test.xlsx')
df = pd.DataFrame(file_name, columns= ['Date'])
df['Date'] = pd.to_datetime(df.Date)

frmtedDate = df['Date']
today = pd.datetime.now().date()

delta = frmtedDate - today
print(delta)

Solution

  • You can use pd.Timestamp.today and then extract the amount of days from the timedelta which it returns with dt.days:

    # Example dataframe
    df = pd.DataFrame({'Date':['20-6-2019', '21-6-2019', '25-6-2019']})
    df['Date'] = pd.to_datetime(df['Date'])
    
            Date
    0 2019-06-20
    1 2019-06-21
    2 2019-06-25
    

    (df['Date'] - pd.Timestamp.today()).dt.days
    
    0    3
    1    4
    2    8
    Name: Date, dtype: int64
    

    Note in this case 3 days for 20-6-2019 is correct, since there are 3 full days between date now and the 20th of june.