I have a column ("ADMIT_YEAR"
) of integers in a dataframe. A typical element in the column looks like: 200110
, where 2001
= year and 10
= month. I need to convert this column to type datetime.
I succeeded in doing so using the clunky method below. Could someone offer a more efficient way of writing this code?
Freshman['ADMIT_YEAR'] = Freshman['ADMIT_TERM'].astype(str).str.slice(0,4)
Freshman['ADMIT_MONTH'] = Freshman['ADMIT_TERM'].astype(str).str.slice(4,6)
Freshman['ADMIT_DATE_str'] = Freshman['ADMIT_YEAR']+'/'+Freshman['ADMIT_MONTH']
Freshman['ADMIT_DATE'] = pd.to_datetime(Freshman['ADMIT_DATE_str'], format="%Y/%m")
Note: I believe this question is not answered here since my dates are not integer days.