How to convert Pandas Index to month name

If you have a DatetimeIndex, you can use

websiteGroup.rename(index=lambda x: x.strftime('%B'))

.rename can take a function, and we'll use the '%B' code for the full month name.


Use DatetimeIndex.strftime:

websiteGroup.index = websiteGroup.index.strftime('%B')
print (websiteGroup)
               A        B       C
January    18185   805769     NaN
February   73236   944458     NaN
March     101737  1003966     NaN
April     101018   861229     NaN
May        77724   845223     NaN
June      111503   966043     NaN
July      115413   937184     NaN
August    115215   890457  1649.0

df = websiteGroup.set_index(websiteGroup.index.strftime('%b'))
print (df)
          A        B       C
Jan   18185   805769     NaN
Feb   73236   944458     NaN
Mar  101737  1003966     NaN
Apr  101018   861229     NaN
May   77724   845223     NaN
Jun  111503   966043     NaN
Jul  115413   937184     NaN
Aug  115215   890457  1649.0

Also for assign new values in index is possible use set_index:

df = websiteGroup.set_index(websiteGroup.index.strftime('%B'))
print (df)
               A        B       C
January    18185   805769     NaN
February   73236   944458     NaN
March     101737  1003966     NaN
April     101018   861229     NaN
May        77724   845223     NaN
June      111503   966043     NaN
July      115413   937184     NaN
August    115215   890457  1649.0

EDIT:

For versions pandas 0.23.0 is possible use DatetimeIndex.month_name:

websiteGroup.index = websiteGroup.index.month_name()
print (websiteGroup)
               A        B       C
Website                          
January    18185   805769     NaN
February   73236   944458     NaN
March     101737  1003966     NaN
April     101018   861229     NaN
May        77724   845223     NaN
June      111503   966043     NaN
July      115413   937184     NaN
August    115215   890457  1649.0

Tags:

Python

Pandas