-3

I'm trying to convert a str type ( like :"1995-01-09 00:00:00") to date, here is my attempt

for line in ratings_df[['user_id','movie_id','rating','timestamp']].values:
    annee= line[3]
    print datetime.strptime(annee, "%Y-%m-%d %H:%M:%S")

and I got this error:

error

Hamza Lahbabi
  • 29
  • 1
  • 9
  • Please `print annee` to confirm you're working with the kind of value you think you're working with. – deceze Dec 01 '17 at 10:34

3 Answers3

0

If your example date string is accurately copied to your question there is a trailing space character at the end. The format string does not have this space, so strptime() is not able to parse the string. You can strip() the date string beforehand:

for line in ratings_df[['user_id','movie_id','rating','timestamp']].values:
    annee= line[3].strip()
    print datetime.strptime(annee, "%Y-%m-%d %H:%M:%S")

Or you could add the space to the end of the format string. I think stripping is better.

mhawke
  • 84,695
  • 9
  • 117
  • 138
0
>>> annee
'1995-01-09 00:00:00 '
>>> datetime.datetime.strptime(annee, "%Y-%m-%d %H:%M:%S ")
datetime.datetime(1995, 1, 9, 0, 0)

No issues found other than extra space at the end, strip timestamp string and try again

Laxmikant Ratnaparkhi
  • 4,745
  • 5
  • 33
  • 49
-1

Try printing value of the variable annee first. This might solve the problem.

eiram_mahera
  • 950
  • 9
  • 25