Open In App

Get Month from Date in Pandas

Last Updated : 30 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with date columns in a dataset, you often need to extract just the month. This is simple using pandas.to_datetime() along with .dt.month or .dt.month_name(). Below are the most effective methods with examples and outputs.

Using .dt.month to Get Numeric Month

This is the most beginner-friendly method. It involves two steps:

  1. Convert string to datetime using pd.to_datetime().
  2. Extract month using .dt.month.
Python
import pandas as pd

df = pd.DataFrame({'date': ['2020-01-18', '2020-02-20', '2020-03-21']})
df['month'] = pd.to_datetime(df['date']).dt.month
print(df)

Output
         date  month
0  2020-01-18      1
1  2020-02-20      2
2  2020-03-21      3

Explanation: .dt.month returns the month component as integers (1 for january, 2 for february, etc.)

Using .month on a DatetimeIndex

If you're working with a DatetimeIndex, you can directly use .month to get the integer month values.

Python
import pandas as pd

dti = pd.date_range('2020-07-01', periods=4, freq='ME')
print(dti.month)

Output
Index([7, 8, 9, 10], dtype='int32')

Explanation: .month extracts the integer month directly from the index.

Examples of Extracting Months in Pandas

Example 1 : Extracting Month Integers in a DataFrame

Use .dt.month to get the numeric month from a datetime range.

Python
import pandas as pd

df = pd.DataFrame({
    'date_given': pd.date_range('2020-07-01 12:00:00', periods=5)
})
df['month_of_date'] = df['date_given'].dt.month
print(df)

Output:

pandas-datetime-1

Example 2: Extracting Full Month Names Using .dt.month_name()

Use .dt.month_name() to get readable month names instead of numbers.

Python
import pandas as pd

d = ['14 / 05 / 2017', '2017', '07 / 09 / 2017']

frame = pd.DataFrame({
    'date': pd.to_datetime(d, format='mixed', dayfirst=True)
})

frame['month'] = frame['date'].dt.month_name()
print(frame)

Output:

dt-month_name
.dt.month_name()

Next Article

Similar Reads