Last updated: Apr 12, 2024
Reading time·4 min
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
.
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)
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).
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.
dt.quarter
You can also use the dt.quarter
attribute to get a quarter from a Date
in
Pandas.
datetime
objects.dt.quarter
attribute to get the quarter of each date.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)
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.
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.
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)
dt.to_period
You can also use the dt.to_period() method to get a quarter from a date in Pandas.
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)
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
.
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.
You can also use the quarter
attribute if you need to get the quarter of a
single Date
when using Pandas.
import datetime as dt import pandas as pd quarter = pd.Timestamp(dt.date(2023, 9, 20)).quarter print(quarter) # 👉️ 3
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.
You can learn more about the related topics by checking out the following tutorials: