Stacey
Stacey

Reputation: 5107

Reformat Dataframe column to date only format

I have a dataframe (df) with a column 'Date of birth' column:

Date of birth
0       1957-04-30 00:00:00
1       1966-11-10 00:00:00
2       1966-11-10 00:00:00
3       1962-03-28 00:00:00
4       1958-10-28 00:00:00
5       1958-06-04 00:00:00

How can I reformat the column to a date only format? After I reformat I'm going to work out age from a specific date:

Date of birth
0       1957-04-30
1       1966-11-10
2       1966-11-10
3       1962-03-28
4       1958-10-28
5       1958-06-04

I have tried using

df["Date of birth"] = pd.to_datetime(df['Date of birth'], format='%d%b%Y')
df["Date of birth"] = df["Date of birth"].dt.strftime('%m/%d/%Y')

but with no joy.

Upvotes: 0

Views: 28

Answers (1)

DeanLa
DeanLa

Reputation: 1919

After the column becomes a date, use date accessor to access it.

df["Date of birth"] = pd.to_datetime(df['Date of birth']).dt.date

Upvotes: 1

Related Questions