Paul Huynh
Paul Huynh

Reputation: 11

YYMM to date time python

I have a dateframe column in Python that is in the format YYMM. E.g January 1996 is 9601.

I'm having a hard time converting it from 9601 to a useable date time format. I want the new format to be 01-01-1996. Does anyone have any suggestions? I tried pd.to_datetime function but it's not getting the results I'm looking for.

Upvotes: 1

Views: 1295

Answers (1)

jezrael
jezrael

Reputation: 863351

Use to_datetime with parameter format:

df = pd.DataFrame({'col':['9601', '9705']})
df['col'] = pd.to_datetime(df['col'], format='%y%m')
print (df)
         col
0 1996-01-01
1 1997-05-01

Upvotes: 5

Related Questions