Get month and Year from Date in Pandas – Python
Last Updated :
26 Nov, 2021
Pandas is one of the most powerful library in Python which is used for high performance and speed of calculation. It is basically an open-source BSD-licensed Python library. Commonly it is used for exploratory data analysis, machine learning, data visualization in data science, and many more. It has very dynamic and easy to understand syntax which makes users jobs easier and is a boost for developers’ innovations (as pandas is a open-source library).
Let us now start with installing pandas. Following are the commands for installing pandas on Linux, windows or mac directly use:
pip install pandas
For installing pandas on anaconda environment use:
conda install pandas
Lets now load pandas library in our programming environment.
import pandas as pd
Coming to accessing month and date in pandas, this is the part of exploratory data analysis. Suppose we want to access only the month, day, or year from date, we generally use pandas.
Method 1: Use DatetimeIndex.month attribute to find the month and use DatetimeIndex.year attribute to find the year present in the Date.
df['year'] = pd.DatetimeIndex(df['Date Attribute']).year
df['month'] = pd.DatetimeIndex(df['Date Attribute']).month
Here ‘df’ is the object of the dataframe of pandas, pandas is callable as ‘pd’ (as imported), ‘DatatimeIndex()’ is a function in pandas which is used to refer to the date attribute of your dataset, ‘Date Attribute’ is the date column in your data-set (It can be anything ans varies from one data-set to other), ‘year’ and ‘month’ are the attributes for referring to the year and month respectively.
Let’s now look at an example:
Code :
Python3
import pandas as pd
raw_data = { 'name' : [ 'Rutuja' , 'Neeraj' ,
'Renna' , 'Pratik' ],
'age' : [ 20 , 19 , 22 , 21 ],
'favorite_color' : [ 'blue' , 'red' ,
'yellow' , "green" ],
'grade' : [ 88 , 92 , 95 , 70 ],
'birth_date' : [ '01-02-2000' , '08-05-1997' ,
'04-28-1996' , '12-16-1995' ]}
df = pd.DataFrame(raw_data,
index = [ 'Rutuja' , 'Neeraj' ,
'Renna' , 'Pratik' ])
df[ 'year' ] = pd.DatetimeIndex(df[ 'birth_date' ]).year
df[ 'month' ] = pd.DatetimeIndex(df[ 'birth_date' ]).month
df.head()
|
Output:

So in the output it is clearly seen that the last two columns of the data-set are appended and we have separately stored the month and date using pandas.
Method 2: Use datetime.month attribute to find the month and use datetime.year attribute to find the year present in the Date .
df['year'] = df['Date Attribute'].dt.year
df['month'] = df['Date Attribute'].dt.month
Here ‘df’ is the object of the dataframe of pandas, pandas is callable as ‘pd’ (as imported), datetime is callable as ‘dt’ (as imported). ‘Date Attribute’ is the date column in your data-set (It can be anything ans varies from one data-set to other), ‘year’ and ‘month’ are the attributes for referring to the year and month respectively.
Let’s now look at example:
Code:
Python3
import pandas as pd
import datetime as dt
raw_data = { 'Leaders' : [ 'Mahatma Gandhi' , 'Jawaharlal Nehru' ,
'Atal Bihari Vajpayee' , 'Rabindranath Tagore' ],
'birth_date' : [ '10-02-1869' , '11-14-1889' ,
'12-25-1924' , '05-07-1861' ]}
df = pd.DataFrame(raw_data,
index = [ 'Mahatma Gandhi' , 'Jawaharlal Nehru' ,
'Atal Bihari Vajpayee' ,
'Rabindranath Tagore' ])
df[ 'year' ] = df[ 'birth_date' ].dt.year
df[ 'month' ] = df[ 'birth_date' ].dt.month
df.head()
|
Output:

So in the output, it is clearly seen that the last two columns of the data-set are appended and we have separately stored the month and date using pandas.
Similar Reads
Get Month from Date in Pandas
Extracting the month from a date in a dataset involves converting a date column into a format that allows us to access the month component directly. Python, provides several methods to achieve this with ease using pd.to_datetime() and .dt.month. Here's a quick method to illustrate: Using dt.month fr
3 min read
Get Day from date in Pandas - Python
Let's discuss how to get the day from the date in Pandas. There can be various ways for doing the same. Let's go through them with the help of examples for better understanding. Example 1 : Pandas.dt_range takes input as a range of dates and returns a fixed frequency DatetimeIndex. Series.dt.dayofwe
2 min read
Get the day from a date in Pandas
Given a particular date, it is possible to obtain the day of the week on which the date falls. This is achieved with the help of Pandas library and the to_datetime() method present in pandas. In most of the datasets the Date column appears to be of the data type String, which definitely isn't comfor
2 min read
Get Minutes from timestamp in Pandas-Python
Pandas is an open-source library built for Python language. It offers various data structures and operations for manipulating numerical data and time series. Here, let's use some methods provided by pandas to extract the minute's value from a timestamp. Method 1: Use of pandas.Timestamp.minute attri
3 min read
Python | Pandas DatetimeIndex.month
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas DatetimeIndex.month attribute outputs an Index object containing numeric values
2 min read
Python | Pandas DatetimeIndex.is_month_start
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas DatetimeIndex.is_month_start attribute returns a numpy array containing logical
2 min read
Python | Pandas DatetimeIndex.year
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas DatetimeIndex.year attribute outputs an Index object containing the value of ye
2 min read
Python | Pandas DatetimeIndex.is_year_end
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas DatetimeIndex.is_year_end attribute is an indicator for whether the date is the
2 min read
Python | Pandas Timestamp.days_in_month
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.days_in_month attribute return the number of days in the month for th
2 min read
Python | Pandas DatetimeIndex.is_month_end
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas DatetimeIndex.is_month_end attribute returns a numpy array containing logical v
2 min read