-1

2020-03-04

I am extracting this from a file I read into a pandas Dataframe and want to transform it to datetime before I add a column and add the datetime to each row.

How can I transform this string to datetime?

Sorry for the newbie question, never dealt with this before.

Prashant Kumar
  • 2,057
  • 2
  • 9
  • 22
yannickhau
  • 385
  • 1
  • 13
  • Does this answer your question? [Converting string into datetime](https://stackoverflow.com/questions/466345/converting-string-into-datetime) – Sorin Mar 04 '20 at 12:39

2 Answers2

0

Since you only have date value and it is in string form, you can use .strptime() methods of datetime.datetime.

This is how you can do it :

from datetime import datetime
myDate = "2020-03-04"
datetime.strptime(myDate, "%Y-%m-%d")

OutPut -> datetime.datetime(2020, 3, 4, 0, 0)
Prashant Kumar
  • 2,057
  • 2
  • 9
  • 22
0

you can use this code:

import time

time=time.strftime('%m%d%H%M%Y.%S')

so the variable time contains the string content of the time.

Yagav
  • 190
  • 1
  • 7