Home > front end >  time data: My column name does not match format
time data: My column name does not match format

Time:08-14

I am struggling with converting column of objects to "datatime".

Here is my code:

df['Order Date'] = pd.to_datetime(df['Order Date'], format='%m/%d/%y %H:%M')

I got error value:

ValueError: time data 'Order Date' does not match format '%m/%d/%y %H:%M' (match)

If it converts single cells, everything works fine. I want to convert an entire column:

a = pd.to_datetime(df.iloc[3]['Order Date'], format='%m/%d/%y %H:%M')
print(a)

2019-04-12 14:38:00

CodePudding user response:

Use the errors='coerce' keyword to return NaT values where the parsing fails, identify which rows are problematic, fix them and then convert the whole column again.

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

df[df['Order Date'].isnull()]  # identify rows with NaT values
  • Related