在Python中分割跨日历季度的财政季度收入

问题描述 投票:0回答:1

我有按客户、按产品(类型、id、描述)、按 "财政季度id "的销售数据(收入和单位),其中财政季度对这家公司来说是唯一的,而且不是定期的(即每个季度的天数不完全相同)。

我想(我想?)把每一行 "拆成 "两个有效的observationstransactions,把单位和收入的适当份额分配给该财政季度所跨的两个常规日历季度。

我还有一个表(df2),将公司的每个财政季度映射到日历开始和结束日期。

微小的样本。

df1 = pd.DataFrame({'fisc_q_id': ['2013Q1', '2013Q2'], 
                   'cust':['Faux Corp', 'Notaco'], 
                   'prod_id':['ABC-123', 'DEF-456'], 
                   'revenue':[100, 400]})

df2 = pd.DataFrame({'fisc_q_id': ['2013Q1', '2013Q2'], 
                    'fq_start':['2012-07-29', '2012-10-28'], 
                    'fq_end':['2012-10-27', '2013-01-26']})

希望输出的是四行,每行都保持原来的 "财政季度ID",但会增加一列,写上相应的日历季度和该季度的收入分配。

我有一些关于如何工作的想法,但是我的解决方案--如果我可以找到一个--和你们能提供的相比肯定是不优雅的。

python pandas datetime timedelta
1个回答
0
投票

IICU

  #Merge the datframes
df3=df1.merge(df2)
#Coerce dates into datetime
df3.fq_start = pd.to_datetime(df3.fq_start)
df3.fq_end = pd.to_datetime(df3.fq_end)#Calculate the Calender Quarter for strat and end
df3['fq_startquarter'] = pd.PeriodIndex(df3.fq_start, freq='Q')
df3['fq_endquarter'] = pd.PeriodIndex(df3.fq_end, freq='Q')
#Calculate the end date of the first quarter in the date range and hence the day difference on either side of the partition
df3['Qdate'] = df3['fq_start'].dt.to_period("Q").dt.end_time
df3['EndQdate'] = pd.to_datetime(df3['Qdate'], format='%Y-%M-%d')
df3['days1']=(df3['EndQdate']-df3['fq_start']).dt.days+1
df3['days2']=(df3['fq_end']-df3['EndQdate']).dt.days
df3['dys0']=(df3['fq_end']-df3['fq_start']).dt.days
df3.drop(columns=['Qdate','EndQdate'], inplace=True)
#Melt the calculated quarters
df4=pd.melt(df3, id_vars=['fisc_q_id','cust','prod_id','revenue','fq_start','fq_end','days1','days2','dys0'], value_name='CalenderQuarter')
df4.sort_values(by='prod_id', inplace=True)
#Allocate groups to the quarteres to allow allocation of calculated days
df4['daysp']=df4.groupby('prod_id')['CalenderQuarter'].cumcount()+1
#Set conditions and choices and use np.where to conditionally calculate revenue prportions
conditions= (df4['daysp']==1, df4['daysp']==2)
choices=(df4['revenue']*(df4['days1']/df4['dys0']),df4['revenue']*(df4['days2']/df4['dys0']))
df4['revenuep']=np.select(conditions,choices)
#Drop columns not required
df4['revenuep']=np.select(conditions,choices).round(0)

enter image description here

卷曲的一。定的机会,以方法链,使其效率和速度。

© www.soinside.com 2019 - 2024. All rights reserved.