My title is not great because I'm having trouble articulating my question. Basically, I have a DateFrame with transactional data consisting of a few DateTime columns and a value column. I need to apply filters to the dates and sum the resulting values in a new DataFrame.
Here is a simplified version of my DateFrame df:
Sched Week Ship Week Ready Week vals
0 2021-01-04 2021-01-11 2021-01-04 10
1 2021-01-04 2021-01-11 2021-01-04 10
2 2021-01-04 2021-01-04 2021-01-04 2
3 2021-01-07 2021-01-18 2021-01-04 9
4 2021-01-12 2021-01-18 2021-01-11 1
5 2021-01-13 2021-01-11 2021-01-11 6
6 2021-01-13 2021-01-11 2021-01-11 4
7 2021-01-13 2021-01-25 2021-01-11 8
8 2021-01-15 2021-01-25 2021-01-18 4
9 2021-01-19 2021-01-25 2021-01-18 5
10 2021-01-19 2021-01-25 2021-01-18 6
11 2021-01-21 2021-01-25 2021-01-18 10
12 2021-01-21 2021-01-25 2021-01-18 6
The new DataFrame df_result I want to create should look like this based on the values in df. The Sched Week column in this DataFrame is simply df['Sched Week'].unique() and foo is the sum of df['values'] for the rows that meet the conditions below.
Sched Week foo
0 2021-01-04 20
1 2021-01-07 29
2 2021-01-12 10
3 2021-01-13 18
4 2021-01-15 18
5 2021-01-19 23
6 2021-01-21 39
And here is the basic logic to generate the new DataFrame:
df['Sched Week'] <= df_result['Sched Week'] &
df['Ship Week'] > df_result['Sched Week'] &
df['Ready Week'] <= df_result['Sched Week']
This test needs to be performed for each row in the new df_result DataFrame and the values summed.
So, the 20 at index 0 is the sum of the values at index 0 and 1 from the original df, since those rows meet the conditions for 2021-01-04.
I have tried every way to boolean mask and groupby that I can think of but nothing I've done so far has worked.
EDIT
Here is the equivalent in Excel.
The formula in cell J3 is =SUMIFS(F:F,C:C,"<="&I3,D:D,">"&I3,E:E,"<="&I3)
2021-01-19it is correct. It is the sum of values at index 7-10 because that is where all three of the date conditions are True.df_resultis the one I want to create from the one on the leftdf. Anddf_result['Sched Week'] = df['Sched Week'].unique().