0

My dataframe's date column looks like this and contains integers: enter image description here

I want to convert the date column (now containing dtype integers) into %Y%m%d format so that the first row looks like 2002-01-01.

After using the following code:

party_dataset['date'] = party_dataset['date'].apply(lambda x: pd.to_datetime(x))

the column values start to like this 1970-01-01 00:00:00.000002002, 1970-01-01 00:00:00.000002003... (the actual year being sent to the very end).

Can somebody please suggest an example solution code?

Julieta M.
  • 73
  • 5

1 Answers1

0

This this is the answer

import datetime
s = "20120213"
s_datetime = str(datetime.datetime.strptime(s, '%Y%m%d')).split(' ')[0]
print(s_datetime)

another way to get the result

from datetime import datetime
a = '20160228'
print(datetime.strptime(a, '%Y%m%d').strftime('%Y-%m-%d'))