• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:15 mins read
You are currently viewing Pandas Rolling Sum

Pandas DataFrame.rolling(n).sum() function is used to get the sum of rolling windows over a DataFrame. Using this function we can get the rolling sum for single or multiple columns of a given Pandas DataFrame. Where n is the size of window. series.rolling(n).sum() function also is used to calculate the rolling sum for Pandas Series. In this article, I will explain how to calculate the rolling sum of pandas DataFrame and series with examples.

Key Points –

  • Pandas rolling sum calculates the sum of a specified window of values in a pandas DataFrame or Series.
  • It allows for the computation of cumulative sums over a defined period, such as days, months, or other time intervals.
  • The rolling sum function is useful for tasks like trend analysis, identifying patterns, and smoothing data.
  • Parameters include window size, which determines the number of values included in each sum calculation, and optional min_periods, specifying the minimum number of observations required for a valid result.
  • It offers flexibility in specifying window sizes and handling missing values, making it a powerful tool for smoothing data and extracting meaningful insights from sequential datasets.

1. Quick Examples of Rolling Sum

If you are in a hurry, below are some quick examples of the rolling sum of DataFrame and Series.


# Quick examples of pandas rolling sum

# Example 1: Rolling sum of pandas series
ser2 = ser.rolling(3).sum()

# Example 2: Use series.rolling().sum() function
ser2 = ser.rolling(3, center=True).sum()

# Example 3: Rolling sum of single columns
df2 = df['A'].rolling(3).sum()

# Example 4: Use DataFrame.rolling().sum()
# Rolling Sum of multiple columns
df2 = df.rolling(2).sum()

# Example 5: Rolling Sum of multiple columns
df2=df.rolling(window=2).sum()

2. Rolling Sum of Series

Pandas Series is a one-dimensional, Index-labeled data structure available in the Pandas library. It can store all the datatypes such as strings, integers, float, and other Python objects. We can access each element in the Series with the help of corresponding default indices.

Now, let’s create pandas series using a list of values.


import pandas as pd
import numpy as np
# Initialize pandas series
ser = pd.Series([0, 3, 6, 9, 8, 12])
print(ser)

Yields below output.


# Output:
0     0
1     3
2     6
3     9
4     8
5    12
dtype: int64

2.1 Using Series.rolling().sum() Example

You can use Series.rolling() function to get a rolling window over a pandas series and then apply the sum() function to get the rolling sum over the window. Here, 3 is the size of the window you want to use it will return a rolling sum for all the numerical columns in the Series.


# Rolling sum of pandas series
ser2 = ser.rolling(3).sum()
print(ser2)

Yields below output.


# Output:
0     NaN
1     NaN
2     9.0
3    18.0
4    23.0
5    29.0
dtype: float64

Now, let’s do the rolling sum series with window=3. By default, the result is set to the right edge of the window. You can change this to the center of the window by setting center=True.


# Use series.rolling().sum() function
ser2 = ser.rolling(3, center=True).sum()
print(ser2)

Yields below output.


# Output:
0     NaN
1     9.0
2    18.0
3    23.0
4    29.0
5     NaN
dtype: float64

3. Rolling Sum of DataFrame

Now, Let’s create Pandas DataFrame using data from a Python dictionary, where the columns are A and B.


import pandas as pd
import numpy as np
# Initialize pandas dataframe
df = pd.DataFrame({'A': [0,2,4,5,8,10,12],
                   'B': [0,1,3,7,9,np.nan,15]})
print(df)

Yields below output.


# Output:
    A     B
0   0   0.0
1   2   1.0
2   4   3.0
3   5   7.0
4   8   9.0
5  10   NaN
6  12  15.0

3.1 Use DataFrame.rolling().sum()Example

You can get the three rolling sums of the "A" column, you apply the rolling() function with a window size of three and then apply the sum() function. You get NaN values in the first two rows because you cannot calculate the rolling sum as there are no preceding values to make the three windows complete. The third row is 6.0 which is the sum of "A" over the three windows containing 0, 2, and 4.


# Rolling sum of single columns
df2 = df['A'].rolling(3).sum()
print(df2)

Yields below output.


# Output:
0     NaN
1     NaN
2     6.0
3    11.0
4    17.0
5    23.0
6    30.0
Name: A, dtype: float64

3.2 Rolling Sum of Multiple Columns

Similarly, You can use DataFrame.rolling() function to get the two windows rolling sum of multiple columns and then apply the sum() function to get the rolling sum over the window. It will calculate the rolling sum for all the numerical columns in the DataFrame. For example, let’s get the two rolling sums of multiple columns in DataFrame.


# Use DataFrame.rolling().sum()
# Rolling Sum of multiple columns
df2 = df.rolling(2).sum()
print(df2)

# Rolling Sum of multiple columns
df2=df.rolling(window=2).sum()
print(df2)

Yields below output.


# Output:
      A     B
0   NaN   NaN
1   2.0   1.0
2   6.0   4.0
3   9.0  10.0
4  13.0  16.0
5  18.0   NaN
6  22.0   NaN

4. Complete Example For Rolling Sum


import pandas as pd
import numpy as np
# Initialize pandas dataframe
df = pd.DataFrame({'A': [0,2,4,5,8,10,12],
                   'B': [0,1,3,7,9,np.nan,15]})
print(df)

# Rolling sum of single columns
df2 = df['A'].rolling(3).sum()
print(df2)

# Use DataFrame.rolling().sum()
# Rolling Sum of multiple columns
df2 = df.rolling(2).sum()
print(df2)

Frequently Asked Questions on Rolling Sum

What is a rolling sum in Pandas?

A rolling sum in Pandas refers to the calculation of the sum of values over a specified window or period in a DataFrame or Series. It moves through the data frame or series, continuously updating the sum within each window.

How is the window size determined in Pandas rolling sum?

The window size in Pandas rolling sum is specified by the window parameter. This parameter determines the number of consecutive values included in each sum calculation. It can be specified in terms of number of periods (e.g., days, months) or a fixed size (e.g., 5 rows).

Does Pandas rolling sum support different aggregation functions other than sum?

Besides sum, Pandas rolling functionality supports various other aggregation functions such as mean, median, min, max, std (standard deviation), etc. You can specify the desired aggregation function using the agg() method or by directly calling the specific aggregation function on the rolling object.

Can Pandas rolling sum handle missing values?

Pandas rolling sum can handle missing values. By default, it ignores missing values during the calculation. However, you can customize this behavior using parameters such as min_periods to specify the minimum number of non-null values required for a valid result.

What are some common use cases of rolling sum?

Common use cases of Pandas rolling sum include trend analysis, moving averages calculation, identifying patterns or anomalies in time-series data, and smoothing noisy data. It is widely used in financial analysis, signal processing, and other fields dealing with sequential data.

Conclusion

In this article, I have explained how to get the rolling sum of Series and DataFrame by using series.rolling().sum(), and DataFrame.rolling().sum() functions respectively.

Happy Learning !!

Related Articles

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.