1

I am trying to convert the string to the type of 'datetime' in python. My data match the format, but still get the 'ValueError: time data 11 11 doesn't match format specified' I am not sure where does the "11 11" in the error come from.

My code is

train_df['date_captured1'] = pd.to_datetime(train_df['date_captured'], format="%Y-%m-%d %H:%M:%S")

Head of data is

print (train_df.date_captured.head())

0    2011-05-13 23:43:18
1    2012-03-17 03:48:44
2    2014-05-11 11:56:46
3    2013-10-06 02:00:00
4    2011-07-12 13:11:16
Name: date_captured, dtype: object

I tried the following by just selecting the first string and running the code with same datetime format. They all work without problem.

dt=train_df['date_captured']
dt1=dt[0]
date = datetime.datetime.strptime(dt1, "%Y-%m-%d %H:%M:%S")
print(date)

2011-05-13 23:43:18

and

dt1=pd.to_datetime(dt1, format='%Y-%m-%d %H:%M:%S')
print (dt1)

2011-05-13 23:43:18

But why wen I using the same format in pd.to_datetime to convert all the data in the column, it comes up with the error above? Thank you.

2
  • Looks like the error is from line 2 where your date is 2014-05-11 11:56:46 this has the 11 11 do you have anymore information? Commented Apr 16, 2019 at 20:04
  • I solved it! It's not the line 2. But I found in line 100372, the date_captured value is '11 11'. Commented Apr 17, 2019 at 13:26

1 Answer 1

2

I solved it.

train_df['date_time'] = pd.to_datetime(train_df['date_captured'], errors='coerce')
print (train_df[train_df.date_time.isnull()])

I found in line 100372, the date_captured value is '11 11'

        category_id date_captured    ...     height  date_time
100372           10         11 11    ...        747        NaT

So the code with errors='coerce' will replace the invalid parsing with NaN. Thank you.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.