In this tutorial, we’ll try to solve 2 problems:
- Extract the month from a specific cell of dataframe
- Extract the month from all the cells in the column of a dataframe
Datatype conversion:
Before going through the below solutions for the problem, we first need to make sure that the datatype of the date_column for which we need the month has to be pandas datetime. In case you have strings in the column then try to use pandas.to_datetime
method to convert the datatype of the column to pandas datetime.
code:
df['date_column'] = pd.to_datetime(df['date_column'])
The above line of code changes the datatype of the ‘date_column’ column to pandas datetime.
Problem – 1: Extract month from a specific cell of dataframe
Here, we’re trying to extract the month from a specific cell of a pandas dataframe object. To achieve this we need to follow the below steps.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
- Make sure that the datatype of the column is pandas datetime. If not, then use the above step specified in
Datatype conversion
section - Once, you make sure the datatype of the column is pandas datetime, then select the specific cell which you need to extract the month from.
- Then use the
.month
attribute over the selected cell, which gives the month of the date specified by the cell.
Basic Syntax:
df['date_column'] = pd.to_datetime(df['date_column']) df['date_column'][0].month
The above code first converts the data type of the column to pandas datetime and then extracts the month of the specified cell using the .month
attribute which is an inbuilt attribute for the timestamp datatype.
Examples
Now, let us have a look at some of the examples to demonstrate the above solution.
Note:
In case you don’t have any dataframe to try the solution, use the below implementation to create a dataframe with some dates as cells. The below-given dataframe will be used to demonstrate all below examples.
import pandas as pd dates = {'date_column':['14 - 05 - 2017', '2017', '07 - 09 - 2017']} df = pd.DataFrame(dates) df['date_column'] = pd.to_datetime(df['date_column'], dayfirst = True) df
Output:

Example – 1
code:
df['date_column'][0].month
Output:
5
Example – 2
code:
df['date_column'][2].month
Output:
9
Problem – 2: Extract the month from all the cells in the column of a dataframe

Here, we’re trying to extract the month from all the cells in the column of a dataframe object. To achieve this we need to follow the below steps.
- Make sure that the datatype of the column is pandas datetime. If not, then use the above step specified in
Datatype conversion
section - Then use the
.dt.month
attribute over the selected column which gives the month of the all dates in the column.
Basic Syntax:
df['date_column'] = pd.to_datetime(df['date_column']) df['date_column'].dt.month
The above code first converts the data type of the column to pandas datetime and then extracts the month of all the cells in the column using the .dt.month
attribute.
Examples
Now, let us have a look at some of the examples to demonstrate the above solution.
Note:
In case you don’t have any dataframe to try the solution, use the below implementation to create a dataframe with some dates as cells. The below-given dataframe will be used to demonstrate all below examples.
import pandas as pd dates = {'date_column':['14 - 05 - 2017', '2017', '07 - 09 - 2017']} df = pd.DataFrame(dates) df['date_column'] = pd.to_datetime(df['date_column'], dayfirst = True) df
Output:

Example – 1
code:
df['date_column'].dt.month
Output:
0 5 1 1 2 7 Name: date_column, dtype: int64
Summary
In this we looked at 2 different problems and tried to solve each one separately
- Extract the month from a specific cell of dataframe
Solution: Using
.month
attributeResult: Month of the date (integer)
- Extract the month from all the cells in the column of a dataframe
Solution: Using
.dt.month
attributeResult: Series of interger values specifying month of each date in the cells
You might also be interested in –
- Pandas – Change Format of Date Column
- Pandas – Check if Column contains String from List
- Pandas – Category Column with Datetime Values
- Remove Time from Date in Pandas
- Pandas – Check if a column is all one value
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.