# Generate list of N quarters relative to the date in Python

## Question:

I need to generate a list of N future quarters of the year(s) relative to the date, starting from the next quarter in the form Q2-YY, Q3-YY, Q4-YY, Q1-YY+1,etc., where YY – starting year, incrementing if, for example, 10 quarters are required.

## Answers:

You could use list comprehension:

``````start_quarter = 1
start_year = 23
total_quarters = 10

future_quarters = [f"Q{i % 4 + 1}-{start_year + (i // 4)}" for i in range(start_quarter, total_quarters)]

print(future_quarters)
``````

Output:

``````['Q2-23', 'Q3-23', 'Q4-23', 'Q1-24', 'Q2-24', 'Q3-24', 'Q4-24', 'Q1-25', 'Q2-25', 'Q3-25']
``````

Rationale: the quarters need to start from one quarter after the initial one, but modulo 4 gives us the numbers 0 to 3. This nicely works out when just adding 1 to the value after modulo. The year should increment after four quarters, and integer division by 4 gives us that.

You can use pandas period series creating with `period_range` giving a start date and number of periods with a frequency.

``````p = pd.period_range('2020-01-01', periods=12, freq='Q')
ps = p.strftime('Q%q-%y')
ps
``````

Output:

``````Index(['Q1-20', 'Q2-20', 'Q3-20', 'Q4-20', 'Q1-21', 'Q2-21', 'Q3-21', 'Q4-21',
'Q1-22', 'Q2-22', 'Q3-22', 'Q4-22'],
dtype='object')
``````

and

``````df = pd.DataFrame()
df['Quater'] = ps
``````

Output:

``````   Quarter
0    Q1-20
1    Q2-20
2    Q3-20
3    Q4-20
4    Q1-21
5    Q2-21
6    Q3-21
7    Q4-21
8    Q1-22
9    Q2-22
10   Q3-22
11   Q4-22
``````

And, since this is using a real period dtype, you can get the start and end of the quarters:

``````df['Period Start'] = p.start_time
df['Period End'] = p.end_time.normalize()
``````

Output:

``````   Quarter Period End Period Start
0    Q1-20 2020-03-31   2020-01-01
1    Q2-20 2020-06-30   2020-04-01
2    Q3-20 2020-09-30   2020-07-01
3    Q4-20 2020-12-31   2020-10-01
4    Q1-21 2021-03-31   2021-01-01
5    Q2-21 2021-06-30   2021-04-01
6    Q3-21 2021-09-30   2021-07-01
7    Q4-21 2021-12-31   2021-10-01
8    Q1-22 2022-03-31   2022-01-01
9    Q2-22 2022-06-30   2022-04-01
10   Q3-22 2022-09-30   2022-07-01
11   Q4-22 2022-12-31   2022-10-01
``````
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.