Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

csv

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

like image 761
Sanjay Avatar asked Dec 18 '25 02:12

Sanjay


1 Answers

You will need to use need the capital Y, not y

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

Output:

               new
month   
2022-02-01  February
2022-09-10  September
like image 157
Huy Hiep Nguyen Avatar answered Dec 20 '25 16:12

Huy Hiep Nguyen