How to get a Quarter from a Date in Pandas [4 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. How to get a Quarter from a Date in Pandas
  2. Get a Quarter from a Date in Pandas using dt.quarter
  3. Get a Quarter from a Date in Pandas using dt.to_period
  4. Get the quarter of a single Date using Pandas

# How to get a Quarter from a Date in Pandas

Use the pandas.PeriodIndex class to get a quarter from a date in Pandas.

The class takes a freq keyword argument that can be set to Q to get the quarter that corresponds to each date in the DataFrame.

main.py
import pandas as pd df = pd.DataFrame({ 'Name': [ 'Alice', 'Bobby', 'Carl' ], 'Date': [ '2023-01-12', '2023-05-23', '2023-09-21' ] }) df['quarter'] = pd.PeriodIndex(df['Date'], freq='Q') # Name Date quarter # 0 Alice 2023-01-12 2023Q1 # 1 Bobby 2023-05-23 2023Q2 # 2 Carl 2023-09-21 2023Q3 print(df)

get quarter from date in pandas

The code for this article is available on GitHub

We used the pandas.DataFrame class to create a DataFrame from a dictionary.

The pandas.PeriodIndex class represents an immutable ndarray that holds ordinal values that indicate regular periods in time.

The first argument we passed to the class is the period-like data to construct the index with (the Date column).

main.py
df['quarter'] = pd.PeriodIndex(df['Date'], freq='Q') # Name Date quarter # 0 Alice 2023-01-12 2023Q1 # 1 Bobby 2023-05-23 2023Q2 # 2 Carl 2023-09-21 2023Q3 print(df)

The freq argument can be set to one of the available Pandas period strings.

You can view the available values for the freq argument in this section of the docs.

# Get a Quarter from a Date in Pandas using dt.quarter

You can also use the dt.quarter attribute to get a quarter from a Date in Pandas.

  1. Use the pandas.to_datetime class to convert the date column strings to datetime objects.
  2. Use the dt.quarter attribute to get the quarter of each date.
main.py
import pandas as pd df = pd.DataFrame({ 'Name': [ 'Alice', 'Bobby', 'Carl' ], 'Date': [ '2023-01-12', '2023-05-23', '2023-09-21' ] }) df['quarter'] = pd.to_datetime(df['Date']) df['quarter'] = df['quarter'].dt.quarter # Name Date quarter # 0 Alice 2023-01-12 1 # 1 Bobby 2023-05-23 2 # 2 Carl 2023-09-21 3 print(df)

get quarter from date in pandas using dt quarter

The code for this article is available on GitHub

We first used the pandas.to_datetime() method to convert the Date column to datetime.

Once we have a column containing datetime objects, we can access the dt.quarter attribute to get the quarter of each date.

Notice that the output only contains an integer that represents the quarter.

main.py
df['quarter'] = pd.to_datetime(df['Date']) df['quarter'] = df['quarter'].dt.quarter # Name Date quarter # 0 Alice 2023-01-12 1 # 1 Bobby 2023-05-23 2 # 2 Carl 2023-09-21 3 print(df)

We could've also converted the Date column to datetime in place and then created the quarter column based on it.

main.py
import pandas as pd df = pd.DataFrame({ 'Name': [ 'Alice', 'Bobby', 'Carl' ], 'Date': [ '2023-01-12', '2023-05-23', '2023-09-21' ] }) df['Date'] = pd.to_datetime(df['Date']) df['quarter'] = df['Date'].dt.quarter # Name Date quarter # 0 Alice 2023-01-12 1 # 1 Bobby 2023-05-23 2 # 2 Carl 2023-09-21 3 print(df)
The code for this article is available on GitHub

# Get a Quarter from a Date in Pandas using dt.to_period

You can also use the dt.to_period() method to get a quarter from a date in Pandas.

main.py
import pandas as pd df = pd.DataFrame({ 'Name': [ 'Alice', 'Bobby', 'Carl' ], 'Date': [ '2023-01-12', '2023-05-23', '2023-09-21' ] }) df['Date'] = pd.to_datetime(df['Date']) df['quarter'] = df['Date'].dt.to_period('Q') # Name Date quarter # 0 Alice 2023-01-12 2023Q1 # 1 Bobby 2023-05-23 2023Q2 # 2 Carl 2023-09-21 2023Q3 print(df)

get quarter from date using dt to period

The code for this article is available on GitHub

We first converted the values in the Date column to datetime objects.

We are then able to use the dt.to_period method to convert the datetime values to PeriodArray.

main.py
df['Date'] = pd.to_datetime(df['Date']) df['quarter'] = df['Date'].dt.to_period('Q') # Name Date quarter # 0 Alice 2023-01-12 2023Q1 # 1 Bobby 2023-05-23 2023Q2 # 2 Carl 2023-09-21 2023Q3 print(df)

The only argument the method takes is the frequency.

You can view the available values for the freq argument in this section of the docs.

We set the freq argument to "Q" to get the quarter of each date.

# Get the quarter of a single Date using Pandas

You can also use the quarter attribute if you need to get the quarter of a single Date when using Pandas.

main.py
import datetime as dt import pandas as pd quarter = pd.Timestamp(dt.date(2023, 9, 20)).quarter print(quarter) # 👉️ 3

get quarter of single date using pandas

The code for this article is available on GitHub

We used the pandas.Timestamp() method to convert the Python date to a Pandas datetime object.

We can then access the Timestamp.quarter attribute to get the quarter of the year.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.