如何计算pandas中的自定义会计年度?

发布于 2025-01-11 19:05:56 字数 1000 浏览 0 评论 0原文

我有一个如下所示的数据框

app_date
20/3/2017
28/8/2017
18/10/2017
15/2/2017
2/5/2017
11/9/2016

df = pd.read_clipboard()

我们公司的财政年度是从当年的10月到明年的9月

Q1 - Oct to Dec
Q2 - Jan to Mar
Q3 - Apr to Jun
Q4 - July - Sep  

我正在尝试如下所示

tf['app_date'] = pd.to_datetime(tf['app_date'])
tf['act_month'] = pd.DatetimeIndex(tf['app_date']).month
tf['act_year'] = pd.DatetimeIndex(tf['app_date']).year
tf['act_qtr'] = tf['app_date'].dt.to_period('Q').dt.strftime('Q%q')
tf['comp_fis_year'] = np.where(tf['act_month'] >= 9,tf['act_year']+1,tf['act_year'])
tf['comp_fis_qtr'] = tf['app_date'].dt.to_period('Q').add(1).dt.strftime('Q%q') #thanks to jezrael for this trick to get quarter

有没有优雅而有效的方法执行上述操作?主要是根据我们的财政年度(10月到9月)来计算财政年度?

我希望我的输出如下所示

在此处输入图像描述

I have a dataframe like as shown below

app_date
20/3/2017
28/8/2017
18/10/2017
15/2/2017
2/5/2017
11/9/2016

df = pd.read_clipboard()

Our company fiscal year is from October of current year to September of next year

Q1 - Oct to Dec
Q2 - Jan to Mar
Q3 - Apr to Jun
Q4 - July - Sep  

I was trying something like below

tf['app_date'] = pd.to_datetime(tf['app_date'])
tf['act_month'] = pd.DatetimeIndex(tf['app_date']).month
tf['act_year'] = pd.DatetimeIndex(tf['app_date']).year
tf['act_qtr'] = tf['app_date'].dt.to_period('Q').dt.strftime('Q%q')
tf['comp_fis_year'] = np.where(tf['act_month'] >= 9,tf['act_year']+1,tf['act_year'])
tf['comp_fis_qtr'] = tf['app_date'].dt.to_period('Q').add(1).dt.strftime('Q%q') #thanks to jezrael for this trick to get quarter

Is there any elegant and efficient way to do the above? Mainly for calculating the fiscal year based on our financial year (Oct to Sep)?

I expect my output to be like as shown below

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┊风居住的梦幻卍 2025-01-18 19:05:56

IIUC,使用 to_period< /a> 带有 自定义频率

pd.to_datetime(df['app_date'], dayfirst=True).dt.to_period('Q-SEP')

输出:

0    2017Q2
1    2017Q4
2    2018Q1
3    2017Q2
4    2017Q3
5    2016Q4
Name: app_date, dtype: period[Q-SEP]

对于单独的列:

s = pd.to_datetime(df['app_date'], dayfirst=True).dt.to_period('Q-SEP')
s.astype(str).str.extract('(?P<year>\d+)(?P<quarter>Q\d+)')

输出:

   year quarter
0  2017      Q2
1  2017      Q4
2  2018      Q1
3  2017      Q2
4  2017      Q3
5  2016      Q4

Q开始/结束:

df['Q'] = pd.to_datetime(df['app_date'], dayfirst=True).dt.to_period('Q-SEP')
df['Qstart'] = df['Q'].dt.asfreq('D', 's')
df['Qend'] = df['Q'].dt.asfreq('D', 'e')

输出:

     app_date       Q      Qstart        Qend
0   20/3/2017  2017Q2  2017-01-01  2017-03-31
1   28/8/2017  2017Q4  2017-07-01  2017-09-30
2  18/10/2017  2018Q1  2017-10-01  2017-12-31
3   15/2/2017  2017Q2  2017-01-01  2017-03-31
4    2/5/2017  2017Q3  2017-04-01  2017-06-30
5   11/9/2016  2016Q4  2016-07-01  2016-09-30

IIUC, use to_period with a custom frequency:

pd.to_datetime(df['app_date'], dayfirst=True).dt.to_period('Q-SEP')

output:

0    2017Q2
1    2017Q4
2    2018Q1
3    2017Q2
4    2017Q3
5    2016Q4
Name: app_date, dtype: period[Q-SEP]

for separate columns:

s = pd.to_datetime(df['app_date'], dayfirst=True).dt.to_period('Q-SEP')
s.astype(str).str.extract('(?P<year>\d+)(?P<quarter>Q\d+)')

output:

   year quarter
0  2017      Q2
1  2017      Q4
2  2018      Q1
3  2017      Q2
4  2017      Q3
5  2016      Q4

Q start/end:

df['Q'] = pd.to_datetime(df['app_date'], dayfirst=True).dt.to_period('Q-SEP')
df['Qstart'] = df['Q'].dt.asfreq('D', 's')
df['Qend'] = df['Q'].dt.asfreq('D', 'e')

output:

     app_date       Q      Qstart        Qend
0   20/3/2017  2017Q2  2017-01-01  2017-03-31
1   28/8/2017  2017Q4  2017-07-01  2017-09-30
2  18/10/2017  2018Q1  2017-10-01  2017-12-31
3   15/2/2017  2017Q2  2017-01-01  2017-03-31
4    2/5/2017  2017Q3  2017-04-01  2017-06-30
5   11/9/2016  2016Q4  2016-07-01  2016-09-30
谁把谁当真 2025-01-18 19:05:56

您可以使用:

# Create 2 DatetimeIndex instead of a Series (avoid using .dt accessor)
start = pd.to_datetime(df['app_date'].values, dayfirst=False)
end = start + pd.DateOffset(months=3)

cols = ['act_month', 'act_year', 'act_qtr', 'comp_fis_year', 'comp_fis_qtr']
df = df.join(pd.DataFrame([start.month, start.year, 'Q' + start.quarter.astype(str),
                           end.year, 'Q' + end.quarter.astype(str)],
                           index=cols).T)

输出:

>>> df
     app_date act_month act_year act_qtr comp_fis_year comp_fis_qtr
0   3/20/2017         3     2017      Q1          2017           Q2
1   8/28/2017         8     2017      Q3          2017           Q4
2  10/18/2017        10     2017      Q4          2018           Q1
3   2/15/2017         2     2017      Q1          2017           Q2
4    2/5/2017         2     2017      Q1          2017           Q2
5   11/9/2016        11     2016      Q4          2017           Q1

设置:

app_date
3/20/2017
8/28/2017
10/18/2017
2/15/2017
2/5/2017
11/9/2016

df = pd.read_clipboard()

You can use:

# Create 2 DatetimeIndex instead of a Series (avoid using .dt accessor)
start = pd.to_datetime(df['app_date'].values, dayfirst=False)
end = start + pd.DateOffset(months=3)

cols = ['act_month', 'act_year', 'act_qtr', 'comp_fis_year', 'comp_fis_qtr']
df = df.join(pd.DataFrame([start.month, start.year, 'Q' + start.quarter.astype(str),
                           end.year, 'Q' + end.quarter.astype(str)],
                           index=cols).T)

Output:

>>> df
     app_date act_month act_year act_qtr comp_fis_year comp_fis_qtr
0   3/20/2017         3     2017      Q1          2017           Q2
1   8/28/2017         8     2017      Q3          2017           Q4
2  10/18/2017        10     2017      Q4          2018           Q1
3   2/15/2017         2     2017      Q1          2017           Q2
4    2/5/2017         2     2017      Q1          2017           Q2
5   11/9/2016        11     2016      Q4          2017           Q1

Setup:

app_date
3/20/2017
8/28/2017
10/18/2017
2/15/2017
2/5/2017
11/9/2016

df = pd.read_clipboard()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文