'Pandas resample by N weeks with origin date
Have a look at this code. I want to resample dates by 10-week periods. I can't do it properly with 'resample'. I tried the origin parameter but it doesn't actually make any difference. So I wrote a workaround using a 'groupby' - this is how I'd like it to work. Besically I want to be able to set when the first week of a resampled period is. Can it be done with 'resample', without such workarounds?
import pandas as pd
rng = pd.date_range('2021-03-01', '2022-04-01')
origin = pd.Timestamp(2000, 1, 8)
dt = pd.Series(index=rng, data=rng)
# works improperly:
w = dt.resample('10W-FRI', origin=origin).agg(['count', 'min', 'max'])
print(w)
# workaround:
diff = (dt - origin).dt.days // 70 # 70 days = 10 weeks
w = dt.groupby(diff).agg(['count', 'min', 'max'])
print(w)
The result is:
count min max
2021-03-05 5 2021-03-01 2021-03-05
2021-05-14 70 2021-03-06 2021-05-14
2021-07-23 70 2021-05-15 2021-07-23
2021-10-01 70 2021-07-24 2021-10-01
2021-12-10 70 2021-10-02 2021-12-10
2022-02-18 70 2021-12-11 2022-02-18
2022-04-29 42 2022-02-19 2022-04-01
count min max
110 47 2021-03-01 2021-04-16
111 70 2021-04-17 2021-06-25
112 70 2021-06-26 2021-09-03
113 70 2021-09-04 2021-11-12
114 70 2021-11-13 2022-01-21
115 70 2022-01-22 2022-04-01
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
