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
默认每季度频率Q
相当于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
指定最后一个季度在三月最后一天结束的季度期间。
In [86]: pd.PeriodIndex(df['date'], freq='Q-MAR')
Out[86]: PeriodIndex(['2016Q3', '2015Q3', '2016Q4'], dtype='int64', freq='Q-MAR')