Pandas: Convert Month Number to Month Name and vice versa

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
6 min

banner

# Table of Contents

  1. Pandas: Convert Month Number to Month Name
  2. Convert month number to month name (abbreviation) in DataFrame
  3. Pandas: Convert Month Number to Month Name using calendar
  4. Pandas: Convert Month Number to Month Abbreviation using calendar
  5. Convert Month name to Month number in Pandas

Note: if you need to convert month name to month number, click on the last subheading.

# Pandas: Convert Month Number to Month Name

To convert month number to month name in Pandas:

  1. Use the pandas.to_datetime() method to convert the column to a datetime object.
  2. Use the dt.month_name() method to get the corresponding month names.
main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'start_month': [1, 2, 4, 7, 9], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) df['start_month'] = pd.to_datetime( df['start_month'], format='%m').dt.month_name() # name start_month salary # 0 Alice January 175.1 # 1 Bobby February 180.2 # 2 Carl April 190.3 # 3 Dan July 205.4 # 4 Ethan September 210.5 print(df)

convert month number to month name in pandas

The code for this article is available on GitHub

The pandas.to_datetime() method converts the supplied argument to datetime.

We also passed the format argument to specify that the values in the column are month numbers.

main.py
df['start_month'] = pd.to_datetime( df['start_month'], format='%m').dt.month_name() # name start_month salary # 0 Alice January 175.1 # 1 Bobby February 180.2 # 2 Carl April 190.3 # 3 Dan July 205.4 # 4 Ethan September 210.5 print(df)

You can read more about format strings in this section of the docs.

The last step is to use the dt.month_name() method to get the month names.

# Convert month number to month name (abbreviation) in DataFrame

If you only want to get the abbreviation (first 3 letters), use the str.slice() method after calling month_name().

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'start_month': [1, 2, 4, 7, 9], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) df['start_month'] = pd.to_datetime( df['start_month'], format='%m' ).dt.month_name().str.slice(stop=3) # name start_month salary # 0 Alice Jan 175.1 # 1 Bobby Feb 180.2 # 2 Carl Apr 190.3 # 3 Dan Jul 205.4 # 4 Ethan Sep 210.5 print(df)

convert month number to month abbreviation in dataframe

The code for this article is available on GitHub

The code sample uses the str.slice() method to only get the first 3 letters of each month's name.

You can also use string slicing to achieve the same result.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'start_month': [1, 2, 4, 7, 9], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) df['start_month'] = pd.to_datetime( df['start_month'], format='%m' ).dt.month_name().str[:3] # name start_month salary # 0 Alice Jan 175.1 # 1 Bobby Feb 180.2 # 2 Carl Apr 190.3 # 3 Dan Jul 205.4 # 4 Ethan Sep 210.5 print(df)

The syntax for string slicing is string[start:stop:step].

We omitted the start index, so the slice starts at index 0 and goes up to, but not including index 3.

# Pandas: Convert Month Number to Month Name using calendar

You can also use the built-in calendar module to convert month number to month name in a Pandas DataFrame.

main.py
import calendar import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'start_month': [1, 2, 4, 7, 9], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) df['start_month'] = df['start_month'].apply( lambda x: calendar.month_name[x] ) # name start_month salary # 0 Alice January 175.1 # 1 Bobby February 180.2 # 2 Carl April 190.3 # 3 Dan July 205.4 # 4 Ethan September 210.5 print(df)

convert month number to month name in pandas using calendar

The code for this article is available on GitHub

The calendar.month_name() attribute is an array that represents the months of the year in the current locale.

main.py
import calendar # ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] print(list(calendar.month_name))

Notice that January is month number (index) 1, so the array has 13 elements where the first element is an empty string.

# Pandas: Convert Month Number to Month Abbreviation using calendar

You can use the calendar.month_abbr() method to convert a month number to month abbreviation.

main.py
import calendar import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'start_month': [1, 2, 4, 7, 9], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) df['start_month'] = df['start_month'].apply( lambda x: calendar.month_abbr[x] ) # name start_month salary # 0 Alice Jan 175.1 # 1 Bobby Feb 180.2 # 2 Carl Apr 190.3 # 3 Dan Jul 205.4 # 4 Ethan Sep 210.5 print(df)

convert month number to month abbreviation using calendar

The code for this article is available on GitHub

The calendar.month_abbr attribute returns an array containing all month abbreviations (first 3 letters).

main.py
import calendar # ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] print(list(calendar.month_abbr))

Notice that Jan is the second array element (index 1).

# Convert Month name to Month number in Pandas

You can also use the calendar module if you need to convert month name to month number in Pandas.

main.py
import calendar import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'start_month': ['Jan', 'Feb', 'Apr', 'Jul', 'Sep'], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) month_dict = dict((v, k) for k, v in enumerate(calendar.month_abbr)) print(month_dict) print('-' * 50) df['start_month'] = df['start_month'].map(month_dict) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
{'': 0, 'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12} -------------------------------------------------- name start_month salary 0 Alice 1 175.1 1 Bobby 2 180.2 2 Carl 4 190.3 3 Dan 7 205.4 4 Ethan 9 210.5

We used the calendar.month_abbr attribute to construct a dictionary containing the month abbreviations and numbers.

The last step is to call the map() method on the column, passing it the dictionary.

If your DataFrame column stores the entire names of the months and not just the abbreviations, use the calendar.month_name attribute when constructing the dictionary.

main.py
import calendar import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'start_month': ['January', 'February', 'April', 'July', 'September'], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) month_dict = dict((v, k) for k, v in enumerate(calendar.month_name)) print(month_dict) print('-' * 50) df['start_month'] = df['start_month'].map(month_dict) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
{'': 0, 'January': 1, 'February': 2, 'March': 3, 'April': 4, 'May': 5, 'June': 6, 'July': 7, 'August': 8, 'September': 9, 'October': 10, 'November': 11, 'December': 12} -------------------------------------------------- name start_month salary 0 Alice 1 175.1 1 Bobby 2 180.2 2 Carl 4 190.3 3 Dan 7 205.4 4 Ethan 9 210.5

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.