问题描述
我有一个带有两个有用列的数据框:1)会计年度,2)日期.我想添加一个新列来显示财政季度.
I have a dataframe with two useful columns 1) fiscal year, 2) date. I want to add a new column which shows the fiscal quarter.
仅供参考-英国财政年度为4月1日至3月31日
FYI - UK Financial year runs from 1 April to 31 March
我的数据如下:
fiscal year date
FY15/16 2015-11-01
FY14/15 2014-10-01
FY15/16 2016-02-01
我希望它看起来像这样:
I want it to look like this:
fiscal year date Quarter
FY15/16 2015-11-01 q3
FY14/15 2014-10-01 q3
FY15/16 2016-02-01 q4
真的希望我能找到正确的住所!
Really hope I got the quarters right!
下面的代码有效,但我相信它会返回美国的金融季度,但我想要英国.
Code below works but I believe it returns American financial quarters but I want UK.
df['Quater'] = df['Date'].dt.quarter
推荐答案
import pandas as pd
df = pd.DataFrame({'date': ['2015-11-01', '2014-10-01', '2016-02-01'],
'fiscal year': ['FY15/16', 'FY14/15', 'FY15/16']})
df['Quarter'] = pd.PeriodIndex(df['date'], freq='Q-MAR').strftime('Q%q')
print(df)
收益
date fiscal year Quarter
0 2015-11-01 FY15/16 Q3
1 2014-10-01 FY14/15 Q3
2 2016-02-01 FY15/16 Q4
默认的季度频率Q
等效于Q-DEC
.
The default quarterly frequency Q
is equivalent to Q-DEC
.
In [60]: pd.PeriodIndex(df['date'], freq='Q')
Out[60]: PeriodIndex(['2015Q4', '2014Q4', '2016Q1'], dtype='int64', freq='Q-DEC')
Q-DEC
指定每个季度的季度,其最后一个季度在12月的最后一天结束.Q-MAR
指定每个季度的季度,其最后一个季度在三月的最后一天结束.
Q-DEC
specifies quarterly periods whose last quarter ends on the last day in December.Q-MAR
specifies quarterly periods whose last quarter ends on the last day in March.
In [86]: pd.PeriodIndex(df['date'], freq='Q-MAR')
Out[86]: PeriodIndex(['2016Q3', '2015Q3', '2016Q4'], dtype='int64', freq='Q-MAR')
这篇关于python pandas:从会计年度和月份中获得会计季度(对于英国)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!