1

I have a DataFrame :

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).

Henry Ecker
  • 31,792
  • 14
  • 29
  • 50

2 Answers2

0

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]
Ynjxsjmh
  • 16,448
  • 3
  • 17
  • 42
0
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

Yuca
  • 5,558
  • 3
  • 21
  • 39
Joe Tha
  • 163
  • 9