The Pandas core groupby SeriesGroupBy function rolling() used to roll grouper with provided rolling functionality for each group. It returns a RollingGroupby that includes a new grouper with specified rolling appended.
1 SeriesGroupBy.rolling(*args, **kwargs)
window : It can be integer, timedelta, str, offset or BaseIndexer subclass that specifies the size of the moving window.
1. | If an integer, the fixed number of observations used for each window. |
2. | If a timedelta, str, or offset, the time period of each window. Each window will be a variable sized based on the observations included in the time-period. This is only valid for datetimelike indexes. To learn more about the offsets & frequency strings, please see this link. |
3. | If a BaseIndexer subclass, the window boundaries based on the defined get_window_bounds method. Additional rolling keyword arguments, namely min_periods, center, closed and step will be passed to get_window_bounds. |
min_periods : It is an integer that specifies the minimum number of observations in window required to have a values. Otherwise, result is np.nan
1. | For a window that is specified by an offset, min_periods will default to 1. |
2. | For a window that is specified by an integer, min_periods will default to the size of the window. |
center : It expects a boolean value, If specified value is True, it sets the window labels as the center of the window index, otherwise labels as the right edge of the window edge. If not specified the default value will be False.
win_type : It is a string that specifies a window types, it require additional parameters to be passed in the aggregation function that must match the keywords specified in the Scipy window type method signature. If not specified the default value will be None.
on : It is string that specifies a column label or Index level on which to calculate the rolling window, rather than the DataFrame's index. It ignores the provided integer columns from the result, since an integer index is not used to calculate the rolling window.
closed : It specifies the position from where a calculation starts. If not specified the default value will be None.
1. | right : the first point in the window is excluded from calculations. |
2. | left : the last point in the window is excluded from calculations. |
3. | both : the no points in the window are excluded from calculations. |
4. | neither : the first and last points in the window are excluded from calculations. |
method : It is a string that can be either 'single' or 'table', if not specified the default value will be 'single'. It execute the rolling operation per single column or row ('single') or over the entire object ('table').
rolling() with Series
1 import pandas as pd
2 import numpy as np
3
4 idx = ['A1', 'A2', 'A2', 'A1', 'A3', 'A2', 'A3']
5 ser = pd.Series([3, np.nan, 6, 3, 5, 3, 2], index = idx)
6
7 grouped = ser.groupby(ser.index)
8
9
10 res = grouped.rolling(2).sum()
11
12 print('The rolling of each group :')
13 print(res)
In the above example, a Series object is created by passing an array and index. A SeriesGroupBy function rolling() is called by window size 2 that compute the sum of each groups. The result is assign to the variable that will be printed on console.
1 The rolling of each group :
2 A1 A1 NaN
3 A1 6.0
4 A2 A2 NaN
5 A2 NaN
6 A2 9.0
7 A3 A3 NaN
8 A3 7.0
9 dtype: float64
rolling() with Series and min_periods
rolling() with Series and min_periods
1 import pandas as pd
2 import numpy as np
3
4 idx = ['A1', 'A2', 'A2', 'A1', 'A3', 'A2', 'A3']
5 ser = pd.Series([3, np.nan, 6, 3, 5, 3, 2], index = idx)
6
7 grouped = ser.groupby(ser.index)
8
9
10 res = grouped.rolling(2, min_periods = 1).sum()
11
12 print('The rolling of each group :')
13 print(res)
In the above example, a SeriesGroupBy function rolling() is called by window size 2 and minimum periods that compute the sum of each groups.
1 The rolling of each group :
2 A1 A1 3.0
3 A1 6.0
4 A2 A2 NaN
5 A2 6.0
6 A2 9.0
7 A3 A3 5.0
8 A3 7.0
9 dtype: float64
rolling() with DataFrame
1 import pandas as pd
2 import numpy as np
3
4 df = pd.DataFrame({
5 'A' : ['A1', 'A2', 'A2', 'A1', 'A3', 'A2', 'A3'],
6 'B' : [1, np.nan, 6, 3, 5, 2, 8],
7 'C' : [3, 6, np.nan, 3, 5, 3, np.nan]})
8
9 grouped = df.groupby('A')
10 res = grouped.rolling(2).sum()
11
12 print('The rolling of each group :')
13 print(res)
In the above example, a DataFrame object is created by passing a dictionary. A DataFrameGroupBy function rolling() is called by window size 2 that compute the sum of each groups. The result is assign to the variable that will be printed on console.
1 The rolling of each group :
2 B C
3 A
4 A1 0 NaN NaN
5 3 4.0 6.0
6 A2 1 NaN NaN
7 2 NaN NaN
8 5 8.0 NaN
9 A3 4 NaN NaN
10 6 13.0 NaN
rolling() with min_periods
rolling() with min_periods
1 import pandas as pd
2 import numpy as np
3
4 df = pd.DataFrame({
5 'A' : ['A1', 'A2', 'A2', 'A1', 'A3', 'A2', 'A3'],
6 'B' : [1, np.nan, 6, 3, 5, 2, 8],
7 'C' : [3, 6, np.nan, 3, 5, 3, np.nan]})
8
9 grouped = df.groupby('A')
10 res = grouped.rolling(2, min_periods = 1).sum()
11
12 print('The rolling of each group :')
13 print(res)
In the above example, a DataFrameGroupBy function rolling() is called by window size 2 and minimum periods that compute the sum of each groups.
1 The rolling of each group :
2 B C
3 A
4 A1 0 1.0 3.0
5 3 4.0 6.0
6 A2 1 NaN 6.0
7 2 6.0 6.0
8 5 8.0 3.0
9 A3 4 5.0 5.0
10 6 13.0 5.0
rolling() with on
1 import pandas as pd
2 import numpy as np
3
4 df = pd.DataFrame({
5 'A' : ['A1', 'A2', 'A2', 'A1', 'A3', 'A2', 'A3'],
6 'B' : [1, np.nan, 6, 3, 5, 2, 8],
7 'C' : [3, 6, np.nan, 3, 5, 3, np.nan]})
8
9 grouped = df.groupby('A')
10 res = grouped.rolling(2, on = 'C').sum()
11
12 print('The rolling of each group :')
13 print(res)
In the above example, a DataFrameGroupBy function rolling() is called by window size 2 and on that compute the sum of each groups.
1 The rolling of each group :
2 B C
3 A
4 A1 0 NaN 3.0
5 3 4.0 3.0
6 A2 1 NaN 6.0
7 2 NaN NaN
8 5 8.0 3.0
9 A3 4 NaN 5.0
10 6 13.0 NaN
Related options for your search