Home > Mobile >  Convert Daily dates to weekly using a specific first day of the week
Convert Daily dates to weekly using a specific first day of the week

Time:02-23

I am currently working on grouping/aggregating data based on date range for a weekly plot.

Below is how my dataframe looks like for Daily data:

daily_dates registered attended
02/10/2022 0 0
02/09/2022 0 0
02/08/2022 1 0
02/07/2022 1 0
02/06/2022 20 06
02/05/2022 05 03
02/04/2022 15 12
02/03/2022 10 08
02/02/2022 10 05

The first day of the week I'd want is Sunday.

My current code to perform weekly group is:

weekly_df = weekly_df.resample('w').sum().reset_index()

The output I am desiring is:

weekly_dates registered attended
02/06/2022 22 06
01/30/2022 40 28

A bit of explanation about desired output - the reason for 02/06/2022 & 01/30/2022 is that both these dates are start date of that respective week which is a sunday. And for the week of 01/30/2022 only 02/05/2022|05|03, 02/04/2022, 02/03/2022, 02/02/2022 dates are considered as those are the one's in the daily dataframe.

My current implementation follows the instructions provided here.

I am looking for any suggestion to achieve my Desired Output

CodePudding user response:

Try:

df.resample('W-SUN', label='left', closed='left').sum().reset_index()

Output:

  daily_dates  registered  attended
0  2022-01-30          40        28
1  2022-02-06          22         6
  • Related