Site icon Analytics4All

Python: Convert Datetime to Date using Pandas

Advertisements

To convert a datetime in a pandas dataframe to date use the dt.date function:

df['column'] = pd.to_datetime(df['column']).dt.date

To demonstrate, first let’s build a dataframe

import pandas as pd
df = pd.DataFrame({'Job_Start': ['Demolition','Construction', 'Cleanup'],
                   'time': ['2022-05-20 08:07:22', '2022-05-27 07:34:01', 
                   '2022-06-01 09:12:11']})

Now lets convert the “time” column to date instead of datetime

df['time'] = pd.to_datetime(df['time']).dt.date
Exit mobile version