首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用熊猫显示给定日期的季度

如何使用熊猫显示给定日期的季度
EN

Stack Overflow用户
提问于 2021-04-12 16:58:47
回答 1查看 218关注 0票数 4

我正在尝试生成一个4-4-5日历。我有一个从29-03-20202022-04-09的日期的数据,由它们的等效周数组成。29-03-2020是财政年度的开始日期。我正试图生成一个列,其中包含它们各自所属的部分。

这是我要找的最后一个df,

代码语言:javascript
复制
    a      |  count  | quarter
2020-04-04 |    1    | Q1 2021
2020-04-11 |    2    | Q1 2021
.
.
.
2021-03-27 |    52   | Q4 2021
2021-04-03 |    53   | Q4 2021           #since 2020 is a leap year there are 53 weeks otherwise it will be 52 weeks
2021-04-10 |    1    | Q1 2022
2021-04-17 |    2    | Q1 2022
.
2022-03-02 |    52   | Q4 2022
2022-04-09 |    1    | Q1 2023

我做了以下尝试,

代码语言:javascript
复制
import pandas as pd
import numpy as np
year_start = '2020-03-29'
year_end = '2022-04-09'
week_end_sat = pd.DataFrame(pd.date_range(year_start, year_end, freq=f'W-SAT'), columns=['a'])
first_day_of_year = week_end_sat.iloc[0, 0].replace(day=1, month=1)
baseline = pd.DataFrame(pd.date_range(first_day_of_year, periods=len(week_end_sat), freq=f'W-SAT'), columns=['a'])
week_end_sat['count'] = baseline['a'].dt.isocalendar().week
week_end_sat['quarter'] = 'Q' + baseline['a'].dt.quarter.astype(str) + ' ' + (baseline['a'].dt.year+1).astype(str)
week_end_sat['b'] = baseline['a']
all_days_df = pd.DataFrame(pd.date_range(year_start, year_end), columns=['a'])
merge_df = pd.merge(all_days_df,week_end_sat, on='a', how='left')
merge_df['count'] = merge_df['count'].bfill()
merge_df['quarter'] = merge_df['quarter'].bfill()
merge_df['week_day'] = merge_df['a'].dt.day_name()

在第52周,也就是Q4 2021之前,我得到的硬币是正确的,但是在那之后,它就搞砸了。在第53周,我得到了Q1 2022等等。应该是Q4 2021年的第53周,因为这是一个闰年?有人能给我指点一下我可以纠正这个问题吗?

编辑

如果我也想为每个日期显示它所属的月份怎么办?

最终的df应该是这样的,

代码语言:javascript
复制
   a       |  count  | quarter | month
2020-04-04 |    1    | Q1 2021 | 2020-04
2020-04-11 |    2    | Q1 2021 | 2020-04
.
.
.
2021-03-27 |    52   | Q4 2021 | 2021-03
2021-04-03 |    53   | Q4 2021 | 2021-03        
2021-04-10 |    1    | Q1 2022 | 2021-04
2021-04-17 |    2    | Q1 2022 | 2021-04
.
2022-03-02 |    52   | Q4 2022 | 2022-03
2022-04-09 |    1    | Q1 2023 | 2022-04
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-13 01:23:53

假设year_start总是一个财政年度的开始,那么您可以使用一个for循环来完成这个任务:

代码语言:javascript
复制
# Generate a week-to-accounting-month mapping
m = np.roll(np.arange(1, 13, dtype='int'), -3)
w = np.tile([4,4,5], 4)

acct_month = {
    index + 1: month
        for index, month in enumerate(np.repeat(m, w))
}
acct_month[53] = 3 # week 53, if exists, always belong to month 3

#
data = []
y, week = year_start.year, 1
month = year_start.replace(month=4, day=1)

# A fiscal year has 53 weeks if it starts on a leap year. 52 weeks otherwise
num_week = lambda: 53 if (y % 4 == 0 and y % 100 != 0) or (y % 400 == 0) else 52

for date in pd.date_range(year_start, year_end, freq='7D'):
    data.append((date + pd.Timedelta(days=6), y+1, week, month))
    
    week += 1
    if week > num_week():
        y += 1
        week = 1
        
    if acct_month[week] != month.month:
        month += pd.offsets.MonthBegin(1)
        
df = pd.DataFrame(data, columns=['week_end', 'fy', 'week_no', 'acct_month'])

# Q1, Q2 and Q3 always have 13 weeks
# Q4 may have 13 or 14 weeks
q = np.ceil(df['week_no'].div(13).clip(upper=4)).astype('int')
df['quarter'] = 'Q' + q.astype(str) + ' ' + df['fy'].astype(str)

结果:

代码语言:javascript
复制
      week_end    fy  week_no acct_month  quarter
0   2020-04-04  2021        1 2020-04-01  Q1 2021
1   2020-04-11  2021        2 2020-04-01  Q1 2021
2   2020-04-18  2021        3 2020-04-01  Q1 2021
3   2020-04-25  2021        4 2020-04-01  Q1 2021
...
48  2021-03-06  2021       49 2021-03-01  Q4 2021
49  2021-03-13  2021       50 2021-03-01  Q4 2021
50  2021-03-20  2021       51 2021-03-01  Q4 2021
51  2021-03-27  2021       52 2021-03-01  Q4 2021
52  2021-04-03  2021       53 2021-03-01  Q4 2021
53  2021-04-10  2022        1 2021-04-01  Q1 2022
54  2021-04-17  2022        2 2021-04-01  Q1 2022
55  2021-04-24  2022        3 2021-04-01  Q1 2022
...
100 2022-03-05  2022       48 2022-03-01  Q4 2022
101 2022-03-12  2022       49 2022-03-01  Q4 2022
102 2022-03-19  2022       50 2022-03-01  Q4 2022
103 2022-03-26  2022       51 2022-03-01  Q4 2022
104 2022-04-02  2022       52 2022-03-01  Q4 2022
105 2022-04-09  2023        1 2022-04-01  Q1 2023
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67062561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档