Python 将 Pandas 中的时间序列重新采样为每周间隔
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14530556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resample time series in pandas to a weekly interval
提问by 2daaa
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.
如何将 Pandas 中的时间序列重新采样为每周频率,其中几周从任意一天开始?我看到有一个可选的关键字基础,但它只适用于短于一天的间隔。
采纳答案by Andy Hayden
You can pass anchored offsetsto resample, among other options they cover this case.
您可以将锚定偏移量传递给resample,以及它们涵盖这种情况的其他选项。
For example the weekly frequency from Monday:
例如从星期一开始的每周频率:
ts.resample('W-MON')
回答by denfromufa
You will be much safer with resampling based on days and then slicing every 7th day, e.g:
根据天数重新采样然后每 7 天切片一次,您会更安全,例如:
ts.resample('D').interpolate()[::7]
See the underlying problem with other approaches in this open pandas issue on github:
在 github 上的这个 open pandas issue 中查看其他方法的潜在问题:
回答by Sandro
Neither Andy Haydens nor denfromufas answer worked for me but that did:
df.resample('W', label='left', loffset=pd.DateOffset(days=1))
安迪·海登斯 (Andy Haydens) 和 denfromufas 的回答都不适合我,但确实如此:
df.resample('W', label='left', loffset=pd.DateOffset(days=1))
as described in that answer: https://stackoverflow.com/a/46712821/1743551

