When I first started working with date-related data in Python, one of the most common problems I ran into was converting month names into numbers.
For example, I often received data files where months were written as “January”, “Feb”, or “Sept”. But for analysis, I needed them as numbers like 1, 2, or 9.
Over the years, I’ve learned a few reliable ways to handle this in Python. In this tutorial, I’ll walk you through three simple methods that I use in real projects.
Method 1: Use Python’s datetime Module
The datetime module is my go-to when I want a clean and reliable solution.
Here’s how I do it:
from datetime import datetime
# Example month names
months = ["January", "Feb", "September", "Dec"]
# Convert each month name to number
for m in months:
month_number = datetime.strptime(m, "%B").month if len(m) > 3 else datetime.strptime(m, "%b").month
print(f"{m} -> {month_number}")Output:
January -> 1
Feb -> 2
September -> 9
Dec -> 12You can refer to the screenshot below to see the output.

I like this method because it works with both full month names (January) and abbreviations (Jan, Feb, etc.).
Method 2: Use a Dictionary Mapping
Sometimes, I prefer to create a dictionary when I know the month names will be consistent.
# Dictionary mapping for months
month_map = {
"January": 1, "February": 2, "March": 3, "April": 4,
"May": 5, "June": 6, "July": 7, "August": 8,
"September": 9, "October": 10, "November": 11, "December": 12
}
# Example usage
print(month_map["March"]) # 3
print(month_map["November"]) # 11You can refer to the screenshot below to see the output.

This method is very fast and doesn’t require importing any external libraries. However, it only works with exact matches, so “Sep” won’t work unless I add it to the dictionary.
Method 3: Use Pandas
When I work with large datasets in the U.S. (especially CSV files from government or financial reports), I often use pandas.
Pandas makes it super easy to convert month names to numbers.
import pandas as pd
# Example DataFrame
data = {"Month": ["January", "Feb", "August", "Dec"]}
df = pd.DataFrame(data)
# Convert month names to numbers
df["Month_Number"] = pd.to_datetime(df["Month"], format="%B", errors="coerce").dt.month
print(df)Output:
Month Month_Number
0 January 1
1 Feb 2
2 August 8
3 Dec 12You can refer to the screenshot below to see the output.

I like this method when I’m dealing with spreadsheets or CSV files, because pandas handles messy data gracefully.
Which Method Should You Use?
- If you’re working with single values → use
datetime. - If you want speed and control → use a dictionary.
- If you’re analyzing large datasets → use pandas.
I personally use pandas most often, since much of my work involves real-world data files.
When I first learned Python, I didn’t realize how many ways there were to handle something as simple as months. But once I started working on real-world projects in the U.S., I quickly saw the importance of choosing the right method for the job.
Now, whenever I get messy date data, I know exactly how to clean it up and convert month names into numbers.
You may read:
- Implement the Sigmoid Activation Function in Python
- Use the insert() Function in Python
- Use the round() Function in Python
- Use the Floor() Function in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.