• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / Python / Get Month Name from Datetime in pandas DataFrame

Get Month Name from Datetime in pandas DataFrame

October 17, 2022 Leave a Comment

To get the month name from a datetime column in pandas, you can use the pandas month_name() function. month_name() returns the name of the month as a string.

import pandas as pd

df = pd.DataFrame({
    "date": ["2021-09-30", "2021-12-31", "2022-03-31", "2022-06-30", "2022-09-30", "2022-12-31"],
    "sales": [100,30,50,60,10,80]
})

df["date"] = pd.to_datetime(df["date"])

df["month_name"] = df["date"].dt.month_name()

print(df)

#Output:
        date  sales month_name
0 2021-09-30    100  September
1 2021-12-31     30   December
2 2022-03-31     50      March
3 2022-06-30     60       June
4 2022-09-30     10  September
5 2022-12-31     80   December

When working with data which contains information over time, the ability to extract certain pieces of information from our data easily is valuable.

One such piece of information is getting the month name from a date when using pandas in Python.

To get the month name from a datetime column in pandas, you can use the pandas datetime month_name() function. month_name() returns the name of the month as a string.

Below is a simple example which shows you how to get the name of the month from a datetime columns in a pandas DataFrame.

import pandas as pd

df = pd.DataFrame({
    "date": ["2021-09-30", "2021-12-31", "2022-03-31", "2022-06-30", "2022-09-30", "2022-12-31"],
    "sales": [100,30,50,60,10,80]
})

df["date"] = pd.to_datetime(df["date"])

df["month_name"] = df["date"].dt.month_name()

print(df)

#Output:
        date  sales month_name
0 2021-09-30    100  September
1 2021-12-31     30   December
2 2022-03-31     50      March
3 2022-06-30     60       June
4 2022-09-30     10  September
5 2022-12-31     80   December

Get Month Number from Datetime in pandas

If you want to get the month from a datetime in pandas, instead of the month name, then you can access the pandas datetime “month” property. The “month” property returns a 64-bit integer.

Below is the same DataFrame as above but now we will get the month numbers for each datetime in the date column.

import pandas as pd

df = pd.DataFrame({
    "date": ["2021-09-30", "2021-12-31", "2022-03-31", "2022-06-30", "2022-09-30", "2022-12-31"],
    "sales": [100,30,50,60,10,80]
})

df["date"] = pd.to_datetime(df["date"])

df["month"] = df["date"].dt.month

print(df)

#Output:
        date  sales  month
0 2021-09-30    100      9
1 2021-12-31     30     12
2 2022-03-31     50      3
3 2022-06-30     60      6
4 2022-09-30     10      9
5 2022-12-31     80     12

Hopefully this article has been useful for you to learn how to get the name of the month from a datetime variable in a pandas DataFrame in Python.

Other Articles You'll Also Like:

  • 1.  Check if List is Subset of Another List in Python
  • 2.  Using Python to Find Second Largest Value in List
  • 3.  Draw Star in Python Using turtle Module
  • 4.  How to Concatenate Tuples in Python
  • 5.  Sign Function in Python – Get Sign of Number
  • 6.  Using GroupBy() to Group Pandas DataFrame by Multiple Columns
  • 7.  PROC PHREG Equivalent in Python
  • 8.  Using Python to Read File Character by Character
  • 9.  Creating a Random Color Turtle in Python
  • 10.  Python Get First Word in String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy