ah bon
ah bon

Reputation: 10011

Convert one column to standard date format in Python

For a date column I have data like this: 19.01.01, which means 2019-01-01. Is there a method to change the format from the former to the latter?

My idea is to add 20 to the start of date and replace . with -. Are there better ways to do that?

Thanks.

Upvotes: 2

Views: 48

Answers (1)

jezrael
jezrael

Reputation: 862406

If format is YY.DD.MM use %y.%d.%m, if format is YY.MM.DD use %y.%m.%d in to_datetime:

df = pd.DataFrame({'date':['19.01.01','19.01.02']})

#YY.DD.MM
df['date'] = pd.to_datetime(df['date'], format='%y.%d.%m')
print (df)
        date
0 2019-01-01
1 2019-02-01

#YY.MM.DD
df['date'] = pd.to_datetime(df['date'], format='%y.%m.%d')
print (df)
        date
0 2019-01-01
1 2019-01-02

Upvotes: 4

Related Questions