Python Pandas: How to Get Quarter from Date (e.g., 2025Q1, 1)
Extracting the calendar quarter (Q1, Q2, Q3, Q4) from a date is a common requirement in time series analysis, financial reporting, and business intelligence using Pandas. Whether your dates are strings or already datetime objects, Pandas provides several convenient ways to determine the quarter.
This guide explains how to get the quarter from a date column in a Pandas DataFrame using Series.dt.quarter
, Series.dt.to_period('Q')
, and pd.PeriodIndex
.
Understanding Quarters in Dates
A calendar year is typically divided into four quarters:
- Q1: January, February, March
- Q2: April, May, June
- Q3: July, August, September
- Q4: October, November, December
Pandas can represent the quarter either as an integer (1, 2, 3, or 4) or as a Period
object (e.g., '2025Q1').
Example DataFrame:
import pandas as pd
data = {
'OrderID': [101, 102, 103, 104, 105],
'OrderDate': ['2025-01-15', '2025-05-20', '2025-08-10', '2025-11-05', '2026-02-20'],
'Amount': [100, 150, 200, 50, 120]
}
df_original = pd.DataFrame(data)
print("Original DataFrame:")
print(df_original)
print()
print("Original dtypes:")
print(df_original.dtypes)
Output:
Original DataFrame:
OrderID OrderDate Amount
0 101 2025-01-15 100
1 102 2025-05-20 150
2 103 2025-08-10 200
3 104 2025-11-05 50
4 105 2026-02-20 120
Original dtypes:
OrderID int64
OrderDate object
Amount int64
dtype: object
Prerequisite: Ensuring Date Column is Datetime Type
Most Pandas date/time operations require the column to be of datetime64
(datetime) type. If your date column is currently stored as strings (object type), convert it first using pd.to_datetime()
.
import pandas as pd
df = pd.DataFrame({
'OrderID': [101, 102, 103, 104, 105],
'OrderDate': ['2025-01-15', '2025-05-20', '2025-08-10', '2025-11-05', '2026-02-20'],
'Amount': [100, 150, 200, 50, 120]
})
# ✅ Convert 'OrderDate' column to datetime objects
df['OrderDate'] = pd.to_datetime(df['OrderDate'])
print("DataFrame after converting 'OrderDate' to datetime:")
print(df)
print()
print("Dtypes after conversion:")
print(df.dtypes)
Output:
DataFrame after converting 'OrderDate' to datetime:
OrderID OrderDate Amount
0 101 2025-01-15 100
1 102 2025-05-20 150
2 103 2025-08-10 200
3 104 2025-11-05 50
4 105 2026-02-20 120
Dtypes after conversion:
OrderID int64
OrderDate datetime64[ns]
Amount int64
dtype: object
Method 1: Using Series.dt.quarter
(Returns Integer 1-4)
Once your date column is of datetime64
type, you can access the .dt
accessor, which provides many date/time properties and methods. The .dt.quarter
attribute returns the quarter of the date as an integer (1, 2, 3, or 4).
import pandas as pd
df = pd.DataFrame({
'OrderID': [101, 102, 103, 104, 105],
'OrderDate': pd.to_datetime(['2025-01-15', '2025-05-20', '2025-08-10', '2025-11-05', '2026-02-20']),
'Amount': [100, 150, 200, 50, 120]
})
# ✅ Get the quarter as an integer
df['Quarter_Int'] = df['OrderDate'].dt.quarter
print("DataFrame with integer quarter:")
print(df)
Output:
DataFrame with integer quarter:
OrderID OrderDate Amount Quarter_Int
0 101 2025-01-15 100 1
1 102 2025-05-20 150 2
2 103 2025-08-10 200 3
3 104 2025-11-05 50 4
4 105 2026-02-20 120 1
This is a very direct and common way to get the numerical quarter.
Method 2: Using Series.dt.to_period('Q')
(Returns Period Object like '2025Q1')
The Series.dt.to_period(freq)
method converts a Series of datetime objects into Period
objects representing a span of time (like a quarter, month, or year). Using freq='Q'
converts each date to its corresponding quarter period.
import pandas as pd
df = pd.DataFrame({
'OrderID': [101, 102, 103, 104, 105],
'OrderDate': pd.to_datetime(['2025-01-15', '2025-05-20', '2025-08-10', '2025-11-05', '2026-02-20']),
'Amount': [100, 150, 200, 50, 120]
})
# ✅ Get the quarter as a Period object (e.g., '2025Q1')
df['Quarter_Period'] = df['OrderDate'].dt.to_period('Q')
print("DataFrame with Period quarter:")
print(df)
Output:
DataFrame with Period quarter:
OrderID OrderDate Amount Quarter_Period
0 101 2025-01-15 100 2025Q1
1 102 2025-05-20 150 2025Q2
2 103 2025-08-10 200 2025Q3
3 104 2025-11-05 50 2025Q4
4 105 2026-02-20 120 2026Q1
The dtype
of the 'Quarter_Period' column will be period[Q-DEC]
, indicating quarterly periods ending in December. This format is often useful for time series analysis and grouping by quarter.
Method 3: Using pd.PeriodIndex(series, freq='Q')
This method is similar to dt.to_period('Q')
but operates at the Pandas top level, creating a PeriodIndex
directly. This PeriodIndex
can then be assigned as a new column.
import pandas as pd
df = pd.DataFrame({
'OrderID': [101, 102, 103, 104, 105],
'OrderDate': pd.to_datetime(['2025-01-15', '2025-05-20', '2025-08-10', '2025-11-05', '2026-02-20']),
'Amount': [100, 150, 200, 50, 120]
})
# ✅ Create a PeriodIndex for quarters and assign as a new column
df['Quarter_PeriodIndex'] = pd.PeriodIndex(df['OrderDate'], freq='Q')
print("DataFrame with quarter from PeriodIndex:")
print(df)
Output:
DataFrame with quarter from PeriodIndex:
OrderID OrderDate Amount Quarter_PeriodIndex
0 101 2025-01-15 100 2025Q1
1 102 2025-05-20 150 2025Q2
2 103 2025-08-10 200 2025Q3
3 104 2025-11-05 50 2025Q4
4 105 2026-02-20 120 2026Q1
Both dt.to_period('Q')
and pd.PeriodIndex(..., freq='Q')
produce the same type of quarterly period representation.
Getting the Quarter from a Single Date Object
If you have a single Python datetime.date
or datetime.datetime
object, or a Pandas Timestamp
object, you can get its quarter directly.
import pandas as pd
from datetime import date, datetime
# Python datetime object
py_dt_object = datetime(2025, 7, 25) # July 25th, 2025
quarter_from_py_dt = pd.Timestamp(py_dt_object).quarter
print(f"Quarter from Python datetime {py_dt_object}: {quarter_from_py_dt}")
# Pandas Timestamp object
pd_ts_object = pd.Timestamp('2025-12-01') # December 1st, 2025
quarter_from_pd_ts = pd_ts_object.quarter
print(f"Quarter from Pandas Timestamp {pd_ts_object}: {quarter_from_pd_ts}")
# Get Period object from a single Timestamp
quarter_period_single = pd_ts_object.to_period('Q')
print(f"Quarter Period from Pandas Timestamp: {quarter_period_single}")
Output:
Quarter from Python datetime 2025-07-25 00:00:00: 3
Quarter from Pandas Timestamp 2025-12-01 00:00:00: 4
Quarter Period from Pandas Timestamp: 2025Q4
- Convert Python
datetime
objects topd.Timestamp
first to access the.quarter
attribute or.to_period()
method.
Choosing the Right Method
- For an integer representation (1, 2, 3, 4): Use
your_datetime_series.dt.quarter
. This is simple and direct for numerical quarter values. - For a Period object representation (e.g., '2025Q1', '2025Q2'): Use
your_datetime_series.dt.to_period('Q')
orpd.PeriodIndex(your_datetime_series, freq='Q')
. This format is excellent for time-based indexing, grouping, and resampling by quarter.
Conclusion
Extracting quarter information from dates is a straightforward process in Pandas:
- Ensure your date column is of
datetime64
type usingpd.to_datetime()
. - Use
Series.dt.quarter
to get the quarter as an integer (1-4). - Use
Series.dt.to_period('Q')
orpd.PeriodIndex(series, freq='Q')
to get the quarter as a PandasPeriod
object (e.g., '2025Q1'), which includes the year.
The choice between an integer quarter and a Period object depends on your subsequent analysis needs. Period objects are particularly powerful for time series operations that group by or align with calendar quarters.