1

Say I want to groupby/resample by week. How can I assign the resample index back into the original dataframe like so?

index date
0     1/1/22
1     1/2/22
2     1/3/22
3     1/4/22
4     1/5/22
5     1/6/22
6     1/7/22
7     1/8/22
8     1/9/22
9     1/10/22
index    date    resample_index
0        1/1/22  1/1/22
1        1/2/22  1/1/22
2        1/3/22  1/1/22
3        1/4/22  1/1/22
4        1/5/22  1/1/22
5        1/6/22  1/1/22
6        1/7/22  1/1/22
7        1/8/22  1/8/22
8        1/9/22  1/8/22
9        1/10/22 1/8/22

1 Answers1

1

Check with pd.Grouper with groupby and transform first

df.date = pd.to_datetime(df.date)# here you need to change the date format 
df['new'] = df.groupby(pd.Grouper(key = 'date',freq = '7D'))['date'].transform('first')
df
Out[722]: 
   index       date        new
0      0 2022-01-01 2022-01-01
1      1 2022-01-02 2022-01-01
2      2 2022-01-03 2022-01-01
3      3 2022-01-04 2022-01-01
4      4 2022-01-05 2022-01-01
5      5 2022-01-06 2022-01-01
6      6 2022-01-07 2022-01-01
7      7 2022-01-08 2022-01-08
8      8 2022-01-09 2022-01-08
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Nice, I also tested `df.resample("W-mon", on='date').date.transform("first")` and it worked. If the date were in the index, do I have reset_index? I tried `df.resample("W-mon", on='date').index.transform("first")` and it failed. – Daniel Chang Mar 10 '22 at 03:17
  • @DanielChang df.reset_index().resample("W-mon", on='date').transform("first") – BENY Mar 10 '22 at 03:45
  • Actually, this failed. `df.resample("W-mon", on='date').date.transform("last")` worked though. – Daniel Chang Mar 10 '22 at 10:44