5

1)会計年度、2)日付の2つの有用な列を持つデータフレームがあります。会計四半期を示す新しい列を追加したいと考えています。

参考までに - 英国の会計年度は 4 月 1 日から 3 月 31 日までです。

私のデータは次のようになります:

    fiscal year  date
    FY15/16      2015-11-01
    FY14/15      2014-10-01
    FY15/16      2016-02-01

私はそれを次のようにしたい:

    fiscal year  date        Quarter
    FY15/16      2015-11-01  q3
    FY14/15      2014-10-01  q3
    FY15/16      2016-02-01  q4

クォーターが正しいことを本当に願っています!

以下のコードは機能しますが、アメリカの財務四半期を返すと思いますが、英国が必要です。

df['Quater'] = df['Date'].dt.quarter 
4

1 に答える 1

14
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ます。

In [60]: pd.PeriodIndex(df['date'], freq='Q')
Out[60]: PeriodIndex(['2015Q4', '2014Q4', '2016Q1'], dtype='int64', freq='Q-DEC')

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')
于 2016-06-04T17:09:06.417 に答える