Home > Back-end >  How to use to_date in panda for yyyy-mm-dd format to extract month name?
How to use to_date in panda for yyyy-mm-dd format to extract month name?

Time:04-26

df["month"]=pd.to_datetime(df['date'],format="%y-%m-%d").dt.month_name() 
df.set_index('date', inplace=True)

I used this code to extract month name from the date series in my CSV file. All the dates had a format of yyyy-mm-dd. So i used %y-%m-%d to extract month name from the date. But I'm getting key error. Can u tell me where I made the mistake??

Errors: 0

1

CodePudding user response:

Your format string is incorrect you need to use "%Y-%m-%d". %y is for two digit years, %Y is for four digit years.

you can read more enter image description here

  • Related