Python Dataframe: Date Columns Reformatting "NaTType does not support strftime" Error

1.8k Views Asked by At

I'm trying to reformat the following column names. They all have been converted to datetime columns already.

The code works for the first 3 columns in my list. However, the last two columns breaks it.

date_columns = ['DTI', 'DTO', 'DTD', 'DTE', 'DTINS']

for i in date_columns:
    df_EVENT5_13[i] = df_EVENT5_13[i].apply(lambda x:x.date().strftime('%m/%d/%Y'))

Below is the error I get:

ValueError: NaTType does not support strftime
1

There are 1 best solutions below

0
On

try df_EVENT5_13[i] = df_EVENT5_13[i].dt.date