英文:
Generate list of N quarters of year(s) relative to the date in Python
问题
我可以生成一个相对于当前日期的未来N个季度的列表吗?生成的第一个季度应该是当前日期的下一个季度。需要注意的是,季度必须循环,每年结束时年份必须递增。
例如,在2024年9月,使用输出格式为“Qn-YY”,下一个3个季度将是["Q4-24", "Q1-25", "Q2-25"]。
英文:
How can I generate a list of N future quarters of the year(s) relative to the current date? The first quarter generated should be the next quarter from the current. Notably, the quarter must wrap-around and the year must increment at the end of each year.
For example, with an output format of Qn-YY
on September 2024 the next 3 quarters would be ["Q4-24", "Q1-25", "Q2-25"]
.
答案1
得分: 0
你可以使用列表推导来实现:
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)
输出结果:
['Q2-23', 'Q3-23', 'Q4-23', 'Q1-24', 'Q2-24', 'Q3-24', 'Q4-24', 'Q1-25', 'Q2-25', 'Q3-25']
原因:季度需要从初始季度的下一个季度开始,但取模4会给出0到3的数字。在取模后直接加1可以得到正确的季度数。年份应该在四个季度之后递增,整数除法(//)4可以得到这个结果。
英文:
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.
答案2
得分: 0
你可以使用pandas的period_range
函数创建周期序列,提供起始日期和周期数以及频率。
p = pd.period_range('2020-01-01', periods=12, freq='Q')
ps = p.strftime('Q%q-%y')
ps
输出:
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')
以及
df = pd.DataFrame()
df['Quarter'] = ps
输出:
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
而且,由于这是使用真正的周期数据类型,你可以获取每个季度的起始日期和结束日期:
df['Period Start'] = p.start_time
df['Period End'] = p.end_time.normalize()
输出:
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论