Table of Contents
To extract the quarter from a date in Pandas, you can use the “dt.quarter” function. This function allows you to access the quarter component of a date and return it as a numerical value. It is useful for organizing and analyzing data by quarters, such as in financial or business data. Simply call the “dt.quarter” function on the desired date column in your Pandas dataframe to extract the quarter information. This will allow you to easily group and manipulate data by quarter, providing valuable insights for your analysis.
Pandas: Get Quarter from Date
You can use the following methods to get the quarter from a date in a pandas DataFrame:
Method 1: Get Quarter from Date (Year & Quarter Format)
df['quarter'] = pd.PeriodIndex(df.date, freq='Q')
If the date is in the first quarter of 2022, this will return the quarter in a format like 2022Q1.
Method 2: Get Quarter from Date (Quarter Number Format)
df['quarter'] = df['date'].dt.quarter
If the date is in the first quarter of 2022, this will simply return the value 1.
The following examples shows how to use this syntax in practice with the following pandas DataFrame:
import pandas as pd
#create DataFrame
df = pd.DataFrame({'date': pd.date_range(start='1/1/2022', freq='M', periods=14),
'sales': [6, 8, 10, 5, 4, 8, 8, 3, 5, 14, 8, 3, 10, 12]})
#view DataFrame
print(df)
date sales
0 2022-01-31 6
1 2022-02-28 8
2 2022-03-31 10
3 2022-04-30 5
4 2022-05-31 4
5 2022-06-30 8
6 2022-07-31 8
7 2022-08-31 3
8 2022-09-30 5
9 2022-10-31 14
10 2022-11-30 8
11 2022-12-31 3
12 2023-01-31 10
13 2023-02-28 12Example 1: Get Quarter from Date (Year & Quarter Format)
We can use the following code to create a new column called quarter that extracts the quarter from the date column in a year and quarter format:
#create new column that displays year and quarter from date column
df['quarter'] = pd.PeriodIndex(df.date, freq='Q')
#view updated DataFrame
print(df)
date sales quarter
0 2022-01-31 6 2022Q1
1 2022-02-28 8 2022Q1
2 2022-03-31 10 2022Q1
3 2022-04-30 5 2022Q2
4 2022-05-31 4 2022Q2
5 2022-06-30 8 2022Q2
6 2022-07-31 8 2022Q3
7 2022-08-31 3 2022Q3
8 2022-09-30 5 2022Q3
9 2022-10-31 14 2022Q4
10 2022-11-30 8 2022Q4
11 2022-12-31 3 2022Q4
12 2023-01-31 10 2023Q1
13 2023-02-28 12 2023Q1The new column called quarter contains the quarter from the date column in a year and quarter format.
Example 2: Get Quarter from Date (Quarter Number Format)
We can use the following code to create a new column called quarter that extracts the quarter from the date column in a quarter number format
#create new column that displays quarter from date column
df['quarter'] = df['date'].dt.quarter
#view updated DataFrame
print(df)
date sales quarter
0 2022-01-31 6 1
1 2022-02-28 8 1
2 2022-03-31 10 1
3 2022-04-30 5 2
4 2022-05-31 4 2
5 2022-06-30 8 2
6 2022-07-31 8 3
7 2022-08-31 3 3
8 2022-09-30 5 3
9 2022-10-31 14 4
10 2022-11-30 8 4
11 2022-12-31 3 4
12 2023-01-31 10 1
13 2023-02-28 12 1The new column called quarter contains the quarter number from the date column in a quarter number format.
The following tutorials explain how to perform other common operations in pandas:
Cite this article
stats writer (2024). How can I extract the quarter from a date in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-extract-the-quarter-from-a-date-in-pandas/
stats writer. "How can I extract the quarter from a date in Pandas?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-extract-the-quarter-from-a-date-in-pandas/.
stats writer. "How can I extract the quarter from a date in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-extract-the-quarter-from-a-date-in-pandas/.
stats writer (2024) 'How can I extract the quarter from a date in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-extract-the-quarter-from-a-date-in-pandas/.
[1] stats writer, "How can I extract the quarter from a date in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I extract the quarter from a date in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
