Reputation: 53
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!
Upvotes: 0
Views: 601
Reputation: 164693
This may not be a full response, but it should give you an idea of how to get from 2 datetime
objects to an integer day difference:
import datetime
mdate = '2018-10-05'
rdate = '2010-10-15'
mdate1 = datetime.datetime.strptime(mdate, '%Y-%m-%d').date()
rdate1 = datetime.datetime.strptime(rdate, '%Y-%m-%d').date()
delta = (mdate1 - rdate1).days
print(delta, type(delta))
# output
# 2912 <class 'int'>
Upvotes: 1