我有一個如下所示的資料框
app_date
20/3/2017
28/8/2017
18/10/2017
15/2/2017
2/5/2017
11/9/2016
df = pd.read_clipboard()
我公司會計年度為October當年至September次年
Q1 - Oct to Dec
Q2 - Jan to Mar
Q3 - Apr to Jun
Q4 - July - Sep
我正在嘗試類似下面的東西
tf['app_date'] = pd.to_datetime(tf['app_date'])
tf['act_month'] = pd.DatetimeIndex(tf['app_date']).month
tf['act_year'] = pd.DatetimeIndex(tf['app_date']).year
tf['act_qtr'] = tf['app_date'].dt.to_period('Q').dt.strftime('Q%q')
tf['comp_fis_year'] = np.where(tf['act_month'] >= 9,tf['act_year'] 1,tf['act_year'])
tf['comp_fis_qtr'] = tf['app_date'].dt.to_period('Q').add(1).dt.strftime('Q%q') #thanks to jezrael for this trick to get quarter
有什么優雅而有效的方法來完成上述作業嗎?主要是為了根據我們的財政年度計算財政年度(Oct to Sep)?
我希望我的輸出如下所示

uj5u.com熱心網友回復:
IIUC,使用to_period自定義頻率:
pd.to_datetime(df['app_date'], dayfirst=True).dt.to_period('Q-SEP')
輸出:
0 2017Q2
1 2017Q4
2 2018Q1
3 2017Q2
4 2017Q3
5 2016Q4
Name: app_date, dtype: period[Q-SEP]
對于單獨的列:
s = pd.to_datetime(df['app_date'], dayfirst=True).dt.to_period('Q-SEP')
s.astype(str).str.extract('(?P<year>\d )(?P<quarter>Q\d )')
輸出:
year quarter
0 2017 Q2
1 2017 Q4
2 2018 Q1
3 2017 Q2
4 2017 Q3
5 2016 Q4
Q 開??始/結束:
df['Q'] = pd.to_datetime(df['app_date'], dayfirst=True).dt.to_period('Q-SEP')
df['Qstart'] = df['Q'].dt.asfreq('D', 's')
df['Qend'] = df['Q'].dt.asfreq('D', 'e')
輸出:
app_date Q Qstart Qend
0 20/3/2017 2017Q2 2017-01-01 2017-03-31
1 28/8/2017 2017Q4 2017-07-01 2017-09-30
2 18/10/2017 2018Q1 2017-10-01 2017-12-31
3 15/2/2017 2017Q2 2017-01-01 2017-03-31
4 2/5/2017 2017Q3 2017-04-01 2017-06-30
5 11/9/2016 2016Q4 2016-07-01 2016-09-30
uj5u.com熱心網友回復:
您可以使用:
# Create 2 DatetimeIndex instead of a Series (avoid using .dt accessor)
start = pd.to_datetime(df['app_date'].values, dayfirst=False)
end = start pd.DateOffset(months=3)
cols = ['act_month', 'act_year', 'act_qtr', 'comp_fis_year', 'comp_fis_qtr']
df = df.join(pd.DataFrame([start.month, start.year, 'Q' start.quarter.astype(str),
end.year, 'Q' end.quarter.astype(str)],
index=cols).T)
輸出:
>>> df
app_date act_month act_year act_qtr comp_fis_year comp_fis_qtr
0 3/20/2017 3 2017 Q1 2017 Q2
1 8/28/2017 8 2017 Q3 2017 Q4
2 10/18/2017 10 2017 Q4 2018 Q1
3 2/15/2017 2 2017 Q1 2017 Q2
4 2/5/2017 2 2017 Q1 2017 Q2
5 11/9/2016 11 2016 Q4 2017 Q1
設定:
app_date
3/20/2017
8/28/2017
10/18/2017
2/15/2017
2/5/2017
11/9/2016
df = pd.read_clipboard()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/436864.html
