there is a list of HR:
Department Start End Salary per month
0 Sales 01.01.2020 30.04.2020 1000
1 People 01.05.2020 30.07.2022 3000
2 Marketing 01.02.2020 30.12.2099 3200
3 Sales 01.03.2020 01.08.2023 1200
4 Engineer 01.04.2020 30.12.2099 3500
Using python I'm wondering how to group total salary by month (starting from very beginning of the first employee) and also by the department
I have tried to group salaries by date range but don't know how to also group with the department and show each department in each column
month_starts = pd.date_range(data.start.min(),data.end.max(),freq = 'MS').to_numpy()
contained = np.logical_and(
np.greater_equal.outer(month_starts, data['start'].to_numpy()),
np.less.outer(month_starts,data['end'].to_numpy())
)
masked = np.where(contained, np.broadcast_to(data[['salary_per_month']].transpose(), contained.shape),np.nan)
df = pd.DataFrame(masked, index = month_starts).agg('sum',axis=1).to_frame().reset_index()
df.columns = ['month', 'total_cost']
Expected output: