Skip to main content

__ Back to top

__ ` Ctrl ` \+ ` K `

[ ![pandas 2.2.1 documentation - Home](../_static/pandas.svg) ](../index.html)

Site Navigation

  * [ Getting started ](../getting_started/index.html)
  * [ User Guide ](index.html)
  * [ API reference ](../reference/index.html)
  * [ Development ](../development/index.html)
  * [ Release notes ](../whatsnew/index.html)

  * [ __ GitHub  ](https://github.com/pandas-dev/pandas "GitHub")
  * [ __ Twitter  ](https://twitter.com/pandas_dev "Twitter")
  * [ __ Mastodon  ](https://fosstodon.org/@pandas_dev "Mastodon")

Site Navigation

  * [ Getting started ](../getting_started/index.html)
  * [ User Guide ](index.html)
  * [ API reference ](../reference/index.html)
  * [ Development ](../development/index.html)
  * [ Release notes ](../whatsnew/index.html)

  * [ __ GitHub  ](https://github.com/pandas-dev/pandas "GitHub")
  * [ __ Twitter  ](https://twitter.com/pandas_dev "Twitter")
  * [ __ Mastodon  ](https://fosstodon.org/@pandas_dev "Mastodon")

  * [ 10 minutes to pandas ](10min.html)
  * [ Intro to data structures ](dsintro.html)
  * [ Essential basic functionality ](basics.html)
  * [ IO tools (text, CSV, HDF5, â¦) ](io.html)
  * [ PyArrow Functionality ](pyarrow.html)
  * [ Indexing and selecting data ](indexing.html)
  * [ MultiIndex / advanced indexing ](advanced.html)
  * [ Copy-on-Write (CoW) ](copy_on_write.html)
  * [ Merge, join, concatenate and compare ](merging.html)
  * [ Reshaping and pivot tables ](reshaping.html)
  * [ Working with text data ](text.html)
  * [ Working with missing data ](missing_data.html)
  * [ Duplicate Labels ](duplicates.html)
  * [ Categorical data ](categorical.html)
  * [ Nullable integer data type ](integer_na.html)
  * [ Nullable Boolean data type ](boolean.html)
  * [ Chart visualization ](visualization.html)
  * [ Table Visualization ](style.html)
  * [ Group by: split-apply-combine ](groupby.html)
  * Windowing operations 
  * [ Time series / date functionality ](timeseries.html)
  * [ Time deltas ](timedeltas.html)
  * [ Options and settings ](options.html)
  * [ Enhancing performance ](enhancingperf.html)
  * [ Scaling to large datasets ](scale.html)
  * [ Sparse data structures ](sparse.html)
  * [ Frequently Asked Questions (FAQ) ](gotchas.html)
  * [ Cookbook ](cookbook.html)

  * [ __ ](../index.html)
  * [ User Guide ](index.html)
  * Windowing operations 

#  Windowing operations  #

pandas contains a compact set of APIs for performing windowing operations - an
operation that performs an aggregation over a sliding partition of values. The
API functions similarly to the ` groupby  ` API in that [ ` Series  `
](../reference/api/pandas.Series.html#pandas.Series "pandas.Series") and [ `
DataFrame  ` ](../reference/api/pandas.DataFrame.html#pandas.DataFrame
"pandas.DataFrame") call the windowing method with necessary parameters and
then subsequently call the aggregation function.

    
    
    In [1]: s = pd.Series(range(5))
    
    In [2]: s.rolling(window=2).sum()
    Out[2]: 
    0    NaN
    1    1.0
    2    3.0
    3    5.0
    4    7.0
    dtype: float64
    

The windows are comprised by looking back the length of the window from the
current observation. The result above can be derived by taking the sum of the
following windowed partitions of data:

    
    
    In [3]: for window in s.rolling(window=2):
       ...:     print(window)
       ...: 
    0    0
    dtype: int64
    0    0
    1    1
    dtype: int64
    1    1
    2    2
    dtype: int64
    2    2
    3    3
    dtype: int64
    3    3
    4    4
    dtype: int64
    

##  Overview  #

pandas supports 4 types of windowing operations:

  1. Rolling window: Generic fixed or variable sliding window over the values. 

  2. Weighted window: Weighted, non-rectangular window supplied by the ` scipy.signal  ` library. 

  3. Expanding window: Accumulating window over the values. 

  4. Exponentially Weighted window: Accumulating and exponentially weigh