kamome
kamome

Reputation: 858

How to format a string to datetime column and skip blank values?

I have got a pandas serie as follows:

1    29/04/2020 22:16
2    02/05/2020 15:32
3    01/05/2020 6:37
4    02/05/2020 8:00
5    28/04/2020 8:15
6                    
7    06/05/2020 14:29
8    02/05/2020 8:12

I would like to format to date time type:

df['date_time_column'] = pd.to_datetime(df['date_time_column'], format='%d/%m/%Y %H:%M')

But the error occurs in the 6 row who is blank " "

How can I do that stuff skipping the blank rows error and kepping the row in blank?

Upvotes: 1

Views: 915

Answers (1)

jezrael
jezrael

Reputation: 862851

Add errors='coerce' for converting not matched datetimes to NaT:

df['date_time_column'] = pd.to_datetime(df['date_time_column'], format='%d/%m/%Y %H:%M', errors='coerce')

Upvotes: 3

Related Questions