擅长:python、mysql、java
<p>下面是一个使用<code>DataFrame.rolling</code>的纯熊猫解决方案。它比@BenBoulderite的numpy卷积方法稍微慢一点,但这是一个方便的习惯用法:</p>
<pre><code>df.rolling(interval_length).mean().shift(-(interval_length - 1))
</code></pre>
<p>需要<code>.shift()</code>来对齐滚动平均值,以便结果与滚动窗口的<em>左</em>边对齐,而不是默认的右边(<a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rolling.html" rel="nofollow noreferrer">docs on DataFrame.rolling</a>)。你知道吗</p>