Reputation: 11
How to convert the year-month-day
date format to strptime
format (year/month/day
)?
In[67]
df['Date']=pd.to_datetime(df.birthDate)
df['Date']
Out[67]
0 1985-12-26
1 1977-01-30
2 2003-11-22
3 1983-05-07
4 1983-01-29
...
5640 1956-09-17
5641 1985-12-26
5642 1960-11-23
5643 1928-11-23
5644 1955-12-27
Name: Date, Length: 5645, dtype: datetime64[ns]
above the df I created,so that's my dataset
I originally want to convert it to age because I'm doing a segment of customer report, but the code I applied didn't work, it requires strptime
from datetime import datetime, date
def age(born):
born = datetime.strptime(born, "%Y/%m/%d").date()
today = date.today()
return today.year - born.year - ((today.month,
today.day) < (born.month,
born.day))
df['Age']=df['Date'].apply(age)
display(df)
Upvotes: 1
Views: 541
Reputation: 43
Use the datetime module, here is an example.
import datetime
input_date = "2022-01-09"
date = datetime.datetime.strptime(input_date, "%Y-%m-%d").strftime("%Y/%m/%d")
print(date)
Upvotes: 1