Convert Month Name to Number in Python

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 -> 12

You can refer to the screenshot below to see the output.

dataframe to tensor

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"]) # 11

You can refer to the screenshot below to see the output.

convert dataframe to tensor

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            12

You can refer to the screenshot below to see the output.

tf.convert_to_tensor

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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.