-2

I am getting the error:

 time data '1/1/03 0:00' does not match format '%m/%d/%Y %H:%M' (match)

Does this format not match..? I am new to working with Date-Time formats so this is rather confusing to me.

This is the line of code I am using:

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

Note that the csv file, 'df' that is being used has 1 column named "time", hence I am getting all possible values of it with df['time'].

I should comment that this is:

12/31/16 23:00

is another entry, so I know that it goes month/day/year, is it because the year is only two digits?

Wallace
  • 340
  • 1
  • 3
  • 9

2 Answers2

2

The issue comes from the matching of the year. %Y matches the year with the century so in that case it should be 2003 to be matched. You should use %y instead.

date_time = pd.to_datetime(df['time'], format='%m/%d/%y %H:%M')
itroulli
  • 2,044
  • 1
  • 10
  • 21
1

The problem in your case is the year:

date_time = pd.to_datetime(df['time'], format='%m/%d/%y %H:%M') # the lowercase y fixes it

Its basically the same as in the datetime module:

from datetime import datetime as dt;

dt.strptime('1/1/03 0:00', '%m/%d/%y %H:%M')
>> datetime.datetime(2003, 1, 1, 0, 0)

dt.strptime('1/1/03 0:00', '%m/%d/%Y %H:%M')
>> ## ERROR

All the codes to clarify

Stryder
  • 848
  • 6
  • 9