'How do I remove hours and seconds from my DataFrame column in python? [duplicate]
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).
Solution 1:[1]
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]
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ynjxsjmh |
| Solution 2 | Yuca |
