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.

Free Resources