Home > database >  how do i remove hours and seconds from my data frame column in python/?
how do i remove hours and seconds from my data frame column in python/?

Time:05-07

i have a data frame

Age Gender Address     Date
15    M    172 ST   2022-02-07 00:00:00 

I Want to remove hh:mm:ss I tried -

import datetime as dt
df["Date"]=df["Date"].dt.Date .

But i am receiving no change in date column format.All i want is that the date column has only YYYY-MM-DD .

CodePudding user response:

You can use pd.to_datetime to convert Date column to datetime object.

df['Date'] = pd.to_datetime(df['Date']).dt.date
# or
df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%Y-%m-%d')
# or
df['Date'] = df['Date'].str.split(' ').str[0]

CodePudding user response:

df['Date'] = df['Date'].dt.date

alternatively try datetime.datetime.strptime(when, '%Y-%m-%d').date() Note that this returns a new datetime object -- now remains unchanged.

if all this dont work, try print Date.date(), type(Date.date()) and let me know outputs

  • Related