If all datetime has format MMM-YY
and MMM
is first 3 letters of months names use:
print (final_3)
id earliest_cr_line
0 1077501 Jan-85
1 1077430 Apr-99
2 1077175 Nov-01
3 1076863 Feb-96
4 1075358 Jan-96
final_3['earliest_cr_line'] = pd.to_datetime(final_3['earliest_cr_line'], format='%b-%y')
final_3['diff'] = (pd.to_datetime('2015-06-30') - final_3['earliest_cr_line']).dt.days
print (final_3)
id earliest_cr_line diff
0 1077501 1985-01-01 11137
1 1077430 1999-04-01 5934
2 1077175 2001-11-01 4989
3 1076863 1996-02-01 7089
4 1075358 1996-01-01 7120
EDIT: You can subtract 100 years by some treshold, here is used year > 2021
:
print (final_3)
id earliest_cr_line
0 1077501 Jan-63
1 1077430 Apr-99
2 1077175 Nov-01
3 1076863 Feb-96
4 1075358 Jan-96
final_3['earliest_cr_line'] = pd.to_datetime(final_3['earliest_cr_line'], format='%b-%y')
mask = final_3['earliest_cr_line'].dt.year > 2021
h = pd.DateOffset(years=100)
final_3.loc[mask, 'earliest_cr_line'] = final_3['earliest_cr_line'] - h
final_3['diff'] = (pd.to_datetime('2015-06-30') - final_3['earliest_cr_line']).dt.days
print (final_3)
id earliest_cr_line diff
0 1077501 1963-01-01 19173
1 1077430 1999-04-01 5934
2 1077175 2001-11-01 4989
3 1076863 1996-02-01 7089
4 1075358 1996-01-01 7120
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…