In this tutorial, we will look at how to extract the quarter from date in a pandas series (or column in a dataframe).
How to get quarter from date in Pandas?

There are a number of ways to get the quarter information from a series of dates in pandas. You can use the pandas datetime series’ quarter
property or use the to_period()
function to get the quarter from date. The following is the syntax:
# using the quarter property df['quarter'] = df['date_column'].dt.quarter # using the to_period function df['quarter'] = df['date_column'].dt.to_period('Q')
Let’s look at some examples of using the above techniques. First, we will create a sample pandas dataframe that we will be using throughout this tutorial.
import pandas as pd # create a dataframe df = pd.DataFrame({ "Date": ["2020-01-04", "2020-05-14", "2020-06-21", "2020-08-17", "2020-10-30", "2020-12-25"], "Venue": ["Lisbon", "Madrid", "Barcelona", "London", "Frankfurt", "Paris"] }) # display the dataframe print(df)
Output:
Date Venue 0 2020-01-04 Lisbon 1 2020-05-14 Madrid 2 2020-06-21 Barcelona 3 2020-08-17 London 4 2020-10-30 Frankfurt 5 2020-12-25 Paris
We now have a dataframe containing the Dates and Venues of board meetings of a company in 2020. Before we proceed to use the pandas datetime series properties and functions, let’s convert the “Date” column to datetime in pandas.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
# convert to pandas datetime df['Date'] = pd.to_datetime(df['Date'])
1. Using the pandas.Series.dt.quarter
property
Let’s get the quarter for each date in the “Date” column and store the result as a separate column in the dataframe. We’ll use the quarter
property of each date. This is similar to extracting year or month from dates in pandas.
# using the quarter property df['Quarter'] = df['Date'].dt.quarter # display the dataframe print(df)
Output:
Date Venue Quarter 0 2020-01-04 Lisbon 1 1 2020-05-14 Madrid 2 2 2020-06-21 Barcelona 2 3 2020-08-17 London 3 4 2020-10-30 Frankfurt 4 5 2020-12-25 Paris 4
You can see that we get the quarter for each date as integers. For example, for the date “2020-05-14”, we get the quarter as 2 which is correct because May lies in the 2nd quarter.
2. Using the pandas.Series.dt.to_period()
function
Pass the period that you want to get from the dates. For example, pass 'Q'
for quarter, 'M'
for month, etc. Here’s a list of such strings you can use in pandas. Since we are interested in getting the quarter information, we’ll pass “Q”.
# using the to_period() function df['Quarter_Info'] = df['Date'].dt.to_period('Q') # display the dataframe print(df)
Output:
Date Venue Quarter Quarter_Info 0 2020-01-04 Lisbon 1 2020Q1 1 2020-05-14 Madrid 2 2020Q2 2 2020-06-21 Barcelona 2 2020Q2 3 2020-08-17 London 3 2020Q3 4 2020-10-30 Frankfurt 4 2020Q4 5 2020-12-25 Paris 4 2020Q4
We get the quarter for each date. For example, for “2020-05-14” we got “2020Q2” which tells us that the date lies in the 2nd quarter of the year 2020. This result is more information rich than what we got from the quarter property above (where we got the quarter information as just integers while here we also get the year).
# type of each column df.info()
Output:
<class 'pandas.core.frame.DataFrame'> RangeIndex: 6 entries, 0 to 5 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Date 6 non-null datetime64[ns] 1 Venue 6 non-null object 2 Quarter 6 non-null int64 3 Quarter_Info 6 non-null period[Q-DEC] dtypes: datetime64[ns](1), int64(1), object(1), period[Q-DEC](1) memory usage: 320.0+ bytes
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.