How to obtain the month in a date in pandas

Overview

The month attribute in pandas is used to obtain the value of the month from the given Timestamp object. It is equivalent to Python's datetime object.

Syntax

The month attribute takes the below syntax:

Timestamp.month
Syntax for the month attribute

Parameter value

As an attribute, month takes no parameter value.

Return value

The month attribute returns an int representing the month value of the given date.

Code example

Let's look at the code below:

# A code to illustrate the month attribute in Pandas
# importing the pandas library
import pandas as pd
# creating a date time object
my_date = pd.Timestamp('2022-04-29T7:48:52.192548651')
# obtaining the month value from the given date
print("The month value from the Timestamp object is: ", my_date.month)

Explanation

  • Line 3: We import the Pandas module.
  • Line 6: We create a Timestamp (datetime) object. We name it my_date.
  • Line 9: We obtain the month value from the given Timestamp object, my_date, by using the month attribute.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources