Home > other >  How to change the pd.Grouper function range?
How to change the pd.Grouper function range?

Time:12-07

I am trying to group a bunch of rows by week - start with any date that is a Monday the next six days (to track project progress). Each week should start on a Monday and should include the number of all project update statuses for that Monday until Sunday.

Right now the following code gives me data for Monday and the previous seven days, including the last Monday.

How can I get it to be forward looking instead of backward looking?

DATA SAMPLE:

Date        Status        Proj_ID

2021-09-30  Started       p1
2021-09-30  Started       p3
2021-10-19  Started       p4
2021-10-22  Not Started   p2
2021-10-22  Started       p5

My code returns this (it counts backwards from Monday):

Week         #
2021-10-04   2
2021-10-11   0
2021-10-18   0
2021-10-25   3 

I want it to return this (count forwards from Monday):

Week         #
2021-09-27   2
2021-10-04   0
2021-10-11   0
2021-10-18   3 

My code:

project_statuses = data.groupby(pd.Grouper(key="Date", freq = "W-MON")).size()

CodePudding user response:

Try with resample:

df.set_index('Date', inplace=True)
df = df.resample('W-MON', label='left',closed='left').size()
print(df)

output:

Date
2021-09-27    2
2021-10-04    0
2021-10-11    0
2021-10-18    3
  • Related