Home > Software engineering >  How to convert string to date in pandas df
How to convert string to date in pandas df

Time:05-05

I have a datafram df_cc_su and I want to compare whether two attributes CalculationDateKey and AgreementStartDate are within the same year-month.

CalculationDateKey samples: 20220331, 20220229, 20220132 (string) AgreementStartDate samples: 2022-03-17, 2022-02-27, 2022-01-01 (date format)

I tried to convert the string values in CalculationDateKey to datetime:

df_cc_su['CalculationDateKey_date'] = pd.to_datetime(df_cc_su['CalculationDateKey'], format="%Y/%m/%d")

which resulted in all '1970-01-01 00:00:00.020220331' values.

CodePudding user response:

Use format="%Y%m%d", because not / separator in your data, also 20220132 not exist, so for wrong values are generated NaT if add errors='coerce':

df_cc_su['CalculationDateKey_date'] = pd.to_datetime(df_cc_su['CalculationDateKey'], 
                                                     format="%Y%m%d",
                                                     errors='coerce')
  • Related