Here is my code.
df['Date_Resf_Comp'] = pd.to_datetime(df['Date_Compl'], format="%m/%d/%Y")
df['Curr_Rate_Date'] = pd.to_datetime(df['Curr_Date'], format="%Y-%m-%d")
df['Prev_Rate_Date'] = pd.to_datetime(df['Prev_Date'], format="%Y-%m-%d")
df['Yrs_Sinc_Rsf'] = df.apply(lambda row: (row['Curr_Rate_Date'].year - row['Date_Resf_Comp'].year), axis=1)
df.loc[df['Yrs_Sinc_Rsf'] < 0 , 'Yrs_Sinc_Rsf'] = None
df['Yrs_Since_Rsf_2'] = df.apply(lambda row: row['Yrs_Sinc_Rsf']**2 if row['Yrs_Sinc_Rsf']>=0 else None, axis=1)
df['Yrs_Since_Rsf_3'] = df.apply(lambda row: row['Yrs_Sinc_Rsf']**3 if row['Yrs_Sinc_Rsf']>=0 else None, axis=1)
df = df[["SegID", "Curr_Date", "Prev_Date", "Curr_Rate_Date", "Date_Resf_Comp", "Curr_Distress", "Curr_Rating",
"Prev_Distress", "Prev_Rating", "OFT", "Yrs_Sinc_Rsf","Yrs_Since_Rsf_2", "Yrs_Since_Rsf_3"]]
df
So, I have a dataframe with two columns which I convert to datetime columns. I then try to apply the difference between these two moments in time onto a third, new column. When I use the code above everything functions fine.
The problem is that I do not want just the difference between the years. I'd prefer to get the difference between the dates as a float. In the current case when I take the difference of say datetime.date(2015-1-1) and datetime.date(2015-5-5) use .year my resultant column would display 0 instead of the month difference as a decimal.
I have tried replacing .year with .date() and when I do so I am met with an error on the subsequent line (df.loc...) stating an improper type comparison.
Any help that anyone may be able to provide would be greatly appreciated!