python pandas:从财政年度和月份获得财政季度(对于英国)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37632766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python pandas: get fiscal quarter from fiscal year and month (for UK)
提问by Boosted_d16
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.
我有一个包含两个有用列的数据框 1) 财政年度,2) 日期。我想添加一个显示财政季度的新列。
FYI - UK Financial year runs from 1 April to 31 March
仅供参考 - 英国财政年度从 4 月 1 日到 3 月 31 日
my data looks like:
我的数据看起来像:
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
回答by unutbu
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)
yields
产量
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
The default quarterly frequency Q
is equivalent to Q-DEC
.
默认的季度频率Q
相当于Q-DEC
。
In [60]: pd.PeriodIndex(df['date'], freq='Q')
Out[60]: PeriodIndex(['2015Q4', '2014Q4', '2016Q1'], dtype='int64', freq='Q-DEC')
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.
Q-DEC
指定最后一个季度在 12 月的最后一天结束的季度期间。
Q-MAR
指定最后一个季度在 3 月的最后一天结束的季度期间。
In [86]: pd.PeriodIndex(df['date'], freq='Q-MAR')
Out[86]: PeriodIndex(['2016Q3', '2015Q3', '2016Q4'], dtype='int64', freq='Q-MAR')