Search code examples
pythonpandasdatetimetimedeltapython-dateutil

Python: Difference in dates (column of dates vs. variable date)


I have a column of various dates and wanted to add another column to the dataframe that shows the difference between the dates in the column versus a date in various that I have set.

enter image description here

enddate = date(2021, 10, 15)

Would something like:

enter image description here

I tried below but did returns error:

from dateutil.relativedelta import relativedelta
metric_df['term'] = relativedelta(metric_df['maturity'], enddate).years

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Solution

  • relativedelta doesn't give you fractional years afaik, but you can easily calculate them yourself:

    import pandas as pd
    
    # dummy data
    metric_df = pd.DataFrame({'maturity': ["9/22/2025", "11/10/2033", "3/1/2023"]})
    
    # convert to datetime, so we can run calculations with date & time
    metric_df['maturity'] = pd.to_datetime(metric_df['maturity'])
    
    # to get fractional years, we calculate difference in days and divide
    # by the average number of days per year:
    metric_df['term'] = (metric_df['maturity'] - 
                             pd.Timestamp("2021-10-15")).dt.days / 365.25
    
    metric_df['term']
    0     3.937029
    1    12.071184
    2     1.374401
    Name: term, dtype: float64