0

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!

MapPeddler
  • 53
  • 9
  • Please see: [mcve]. The error message would be useful as well. – sco1 Jan 24 '18 at 20:27
  • @CambrianCatalyst, much of your code is repetitive and not connected with your question. However, for 2 `datetime` objects `a` and `b` the number of days between them would be `(a-b).days`. See https://stackoverflow.com/a/151211/9209546 – jpp Jan 24 '18 at 20:50
  • Hey thanks for your response. I noticed that the reason I seem to be hitting an error is because my new column was getting the difference generated as "x" days instead of just "x". I'm guessing the value type is still that of datetime when I would like it as an int. I will read over your linked reference. Thanks again! – MapPeddler Jan 24 '18 at 20:54

1 Answers1

1

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'>
jpp
  • 159,742
  • 34
  • 281
  • 339