6

I know this question has been asked many times and there are many answers. I followed the previous threads, still not getting my answer. When I did this, without errors= coerce

df['DATE'] = pd.to_datetime(df['Date'], format= "%d-%b-%Y %H:%M:%S")

I get an error:

ValueError: time data '26-Aug-17 10:11:29' does not match format '%d-%b-%Y %H:%M:%S' (match)

As you can see, I think I have the right format. But still it gives error.

When I do with errors= coerce according to this post,

df['DATE'] = pd.to_datetime(df['Date'], format= "%d-%b-%Y %H:%M:%S", `errors= coerce`)

This runs fine, but I get NaT in my DATE column. For example, like this,

        Date                    X           DATE    
  0    26-Aug-17 10:11:29    95.617378      NaT
  1    26-Aug-17 11:11:29    93.617378      NaT
  2    26-Aug-17 12:11:29    91.617378      NaT
  3    26-Aug-17 13:11:29    90.000000      NaT

I have the format correct and still can not find why am I getting this error. Any thoughts will be helpful.

  • @jezrael thanks for suggestion, still getting the same results. No changes! –  Aug 28 '17 at 15:48
  • `print (df['Date'].tolist()[:10])` gives `'26-Aug-17 10:11:29', '26-Aug-17 11:11:29', '26-Aug-17 12:11:29',` –  Aug 28 '17 at 15:49

1 Answers1

14

You need change Y to y, because Y required full year, but y only last 2 digits, see http://strftime.org/:

df['DATE'] = pd.to_datetime(df['Date'], format= "%d-%b-%y %H:%M:%S")
print (df)
                 Date          X                DATE
0  26-Aug-17 10:11:29  95.617378 2017-08-26 10:11:29
1  26-Aug-17 11:11:29  93.617378 2017-08-26 11:11:29
2  26-Aug-17 12:11:29  91.617378 2017-08-26 12:11:29
3  26-Aug-17 13:11:29  90.000000 2017-08-26 13:11:29
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252