pandas 季度_python pandas:从会计年度和月份中获得会计季度(对于英国)

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.

FYI - UK Financial year runs from 1 April to 31 March

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

解决方案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.

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.

In [86]: pd.PeriodIndex(df['date'], freq='Q-MAR')

Out[86]: PeriodIndex(['2016Q3', '2015Q3', '2016Q4'], dtype='int64', freq='Q-MAR')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值