How to Get Month Name from Month Number in Python
In this article, we will learn how to get a month name from a month number in Python. We will use some built-in modules available and some custom codes as well to see them working. Let's first have a quick look over what are dates in Python.
Dates in Python
In Python, we can work on Date functions by importing a built-in module datetime
available in Python. We have date objects to work with dates. This datetime module contains dates in the form of year, month, day, hour, minute, second, and microsecond.
Let us discuss different ways to convert a month number to a month name. We will get the month number by taking input from the user, from date, or by creating a series of dates and then converting the month number to month name. We will see that the month name can be printed in two ways. The first way is the full name of the month as of March and another way is the short name like Mar.
Example: Get Month Name from Month Number using Datetime Object
This method uses datetime
module. The user gives the input for the month number. datetime.strptime()
is called. It takes month number and month format "%m"
as arguments. Passing "%b"
to strftime
returns abbreviated month name while using "%B"
returns full month name.
import datetime
#provide month number
month_num = "3"
datetime_object = datetime.datetime.strptime(month_num, "%m")
month_name = datetime_object.strftime("%b")
print("Short name: ",month_name)
full_month_name = datetime_object.strftime("%B")
print("Full name: ",full_month_name)
Short name: Mar
Full name: March
Example: Get Month Name from Month Number using Calendar Module
This method imports calender
module. It simply uses a for loop to iterate over the number of months (12). There are two built-in arrays in the calendar module. calendar.month_name[]
is an array that represents the full month name while calendar.month_abbr[]
in the second array that represents the abbreviated month names. You can also assign any month number to x in order to find the equivalent month name.
import calendar
for x in range(1,13):
print(x, ":", calendar.month_abbr[x], "-", calendar.month_name[x])
# for month number = 4
x = 4
print(x, ":", calendar.month_abbr[x], "-", calendar.month_name[x])
1 : Jan - January
2 : Feb - February
3 : Mar - March
4 : Apr - April
5 : May - May
6 : Jun - June
7 : Jul - July
8 : Aug - August
9 : Sep - September
10 : Oct - October
11 : Nov - November
12 : Dec - December
4 : Apr - April
Example: Get Month Name using Pandas Series
This method uses Pandas
library of Python. In this example, we create a series of multiple dates to get corresponding month names. This is done by using pd.Series.
This method is useful when you want to convert month numbers to their names from multiple dates. It creates an array for index. It sets the index array to the given series. Pandas dt.month_name()
function returns the month names of the DateTimeIndex with specified locale. Locale determines the language in which the month name is returned. dt.month_name()
returns the names of the month of each timestamp in the given series object.
import pandas as pd
#Create a Series
dates = pd.Series(['2012-12-31', '2019-1-1', '2008-02-2'])
#Create index
idx = ['Date 1', 'Date 2', 'Date 3']
#set the index
dates.index = idx
# Convert the data to datetime
dates = pd.to_datetime(dates)
print(dates)
# return month name
result = dates.dt.month_name(locale = 'English')
print('\n')
print(result)
Date 1 2012-12-31
Date 2 2019-01-01
Date 3 2008-02-02
dtype: datetime64[ns]
Date 1 December
Date 2 January
Date 3 February
dtype: object
Conclusion
In this article, we learned to convert month numbers to month names by using datetime
module, calender
module, and Pandas
library. We used some custom codes as well. We printed abbreviated month names as well as full months names. We get month numbers either as an input or by extracting from a date.