Resample time series in pandas to a weekly interval


Question

How do I resample a time series in pandas to a weekly frequency where the weeks start on an arbitrary day? I see that there's an optional keyword base but it only works for intervals shorter than a day.

1
10
5/18/2017 4:05:15 PM

Accepted Answer

You can pass anchored offsets to resample, among other options they cover this case.

For example the weekly frequency from Monday:

ts.resample('W-MON')
34
9/1/2018 5:38:49 PM

You will be much safer with resampling based on days and then slicing every 7th day, e.g:

ts.resample('D').interpolate()[::7]

See the underlying problem with other approaches in this open pandas issue on github:

https://github.com/pandas-dev/pandas/issues/16381


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon