Last updated: Apr 12, 2024
Reading time·6 min
Note: if you need to convert month name to month number, click on the last subheading.
To convert month number to month name in Pandas:
pandas.to_datetime()
method to convert the column to a datetime
object.dt.month_name()
method to get the corresponding month names.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)
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.
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.
If you only want to get the abbreviation (first 3 letters), use the
str.slice()
method after calling month_name()
.
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)
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.
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
.
calendar
You can also use the built-in calendar
module to convert month number to month
name in a Pandas DataFrame
.
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)
The calendar.month_name() attribute is an array that represents the months of the year in the current locale.
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.
calendar
You can use the calendar.month_abbr() method to convert a month number to month abbreviation.
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)
The calendar.month_abbr
attribute returns an array containing all month
abbreviations (first 3 letters).
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
).
You can also use the calendar
module if you need to convert month name to
month number in Pandas.
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)
Running the code sample produces the following output.
{'': 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.
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)
Running the code sample produces the following output.
{'': 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
You can learn more about the related topics by checking out the following tutorials:
pd.read_json()